mitsest
mitsest

Reputation: 295

Django One-To-Many Model and Admin inlines

I 'm trying to define 2 models in django like so:

class Selector(models.Model):
    # A Beautiful Soup selector
    selector = models.CharField(max_length=ELEMENT_SELECTOR_MAX_LENGTH, null=True, blank=True)

    def __str__(self):
        return self.selector


class Provider(models.Model):
    # Articles' parent container selector
    articles_parent_container_selector = models.ForeignKey(Selector, related_name="articles_parent_container",
                                                          help_text=_("Beautiful Soup selector for all articles' "
                                                                      "parent container"))

    # Article's parent container selector
    article_parent_container_selector = models.ForeignKey(Selector, related_name="article_parent_container_selector",
                                                         help_text=_("Beautiful Soup selector for each article"))

etc. etc. The idea is to have more than one selectors for each field of the Provider model.

What I 'm trying to achieve at the admin application, is have charField inlines for each field of the provider model.

So my admin.py is like

from django.contrib import admin

from .models import Provider, Selector


class SelectorInline(admin.StackedInline):
    model = Selector


class ProviderAdmin(admin.ModelAdmin):
    inlines = [
        SelectorInline,
    ]

admin.site.register(Provider, ProviderAdmin)

I get the error

<class 'news_providers.admin.SelectorInline'>: (admin.E202) 'news_providers.Selector' has no ForeignKey to 'news_providers.Provider'.

I also tried

class SelectorInline(admin.StackedInline):
    model = Selector
    fk_name = 'articles_parent_container'

as described here: Django inline forms with multiple foreign keys

but the error now is:

<class 'news_providers.admin.SelectorInline'>: (admin.E202) 'news_providers.Selector' has no field named 'articles_parent_container'.

Also tried changing my relation to ManyToMany(which seems more relevant to my use-case as well) and apply the hack found here: http://www.mc706.com/tip_trick_snippets/18/django-manytomany-inline-admin/ , but no luck :/

This should be pretty straight forward, but I 'm afraid django developers didn't take notice of this use case?

Thanks!

Upvotes: 3

Views: 1999

Answers (1)

mitsest
mitsest

Reputation: 295

So apparently, there is no built-in functionality to display an inline manyToMany model inside another's model page.

The best you can do is define the model like so

models.py

class Selector(models.Model):
    # A Beautiful Soup selector
    selector = models.CharField(max_length=70, null=True, blank=True)

class Provider(models.Model):

    # Articles' parent container selector
    articles_parent_container_selector = models.ManyToManyField(Selector, blank=True,
                                                                help_text=_("Beautiful Soup selector for all articles' "
                                                                            "parent container."),
                                                                related_name='articles_parent_container')

admin.py

class ArticlesParentContainerSelectorInline(admin.TabularInline):
    model = Provider.articles_parent_container_selector.through
    verbose_name = "Articles' parent container selector"

class ProviderAdmin(admin.ModelAdmin):
    inlines = [
        ArticlesParentContainerSelectorInline,

    ]
    exclude = ('articles_parent_container_selector',)

admin.site.register(Provider, ProviderAdmin)

and what you 'll get looks like this: inline admin for manyToMany

which is a bit of a disappointment, as I was expecting to get Text Inputs instead of dropdowns (or even both of them), so I could add Selectors without having to click the plus sign...

I 'm leaning towards creating my own widget for the admin application.

Anyway, thanks to everyone who bothered to read!

Upvotes: 3

Related Questions