Say I have the following models that have a many-to-many relationship:
class Foo(models.Model):
name = models.TextField()
class Bar(models.Model):
name = models.TextField()
foos = models.ManyToManyField(Foo, related_name='bars')
And then having defined them in admin in the following way:
@admin.register(Foo)
class FooAdmin(admin.ModelAdmin):
"""Foo admin."""
list_display = ('name',)
search_fields = ('name',)
@admin.register(Bar)
class BarAdmin(admin.ModelAdmin):
"""Bar admin."""
list_display = ('name',)
search_fields = ('name',)
In Django admin, when browsing Bar
instances, I can see the Foo
instances Bar
is associated with and can modify them from there.
However, no such luck with Foo
, I can't see the Bar
instances that every Foo object is associated with.
Can Django define automatic handling for this or would I need to roll my own methond?
I'm using Python 3.6.1 and Django 1.11.
Django Admin is one of the most important tools of Django. It's a full-fledged application with all the utilities a developer need. Django Admin's task is to provide an interface to the admin of the web project. Django's Docs clearly state that Django Admin is not made for frontend work.
A ManyToMany field is used when a model needs to reference multiple instances of another model. Use cases include: A user needs to assign multiple categories to a blog post. A user wants to add multiple blog posts to a publication.
NOTE - Documentation on more operations can be found in official Django documentation on many-to-many relationships. Many To Many relationships and representative associative table consists of ForeignKeys, Django only made it easier to implement it with ManyToManyField.
Django ManyToManyField is a field that helps you create many to many relationships in Django. It’s also the most obvious and fastest way. **options – optional arguments explained in official Django documentation ManyToManyField between two models creates a third table in the database. That table contains the primary key of both models.
Django admin interface adds a widget for choosing related objects to a model where ManyToManyField is set. In other words, it requires adding referenced objects first. For our example, you could choose authors only when adding books, but not another way around. After migrating the above models to the database, we get the following database schema:
From the database point of view, it doesn’t matter. Tables are equally connected no matter where you place it. But Django admin and ORM have slight changes in behavior. Django admin interface adds a widget for choosing related objects to a model where ManyToManyField is set. In other words, it requires adding referenced objects first.
You could define a custom InlineModelAdmin
like so:
class BarInline(admin.TabularInline):
model = Bar.foos.through
and use it in your FooAdmin
:
class FooAdmin(admin.ModelAdmin):
"""Foo admin."""
model = Foo
inlines = [
BarInline,
]
Have a look at this part of the django documentation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With