Displaying both sides of a ManyToMany relationship in Django admin

django, django-admin, django-models, manytomanyfield, python

Solution

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.

Problem

Say I have the following models that have a many-to-many relationship: models.py: ``` 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.py ``` @admin.register(Foo) class FooAdmin(admin.ModelAdmin): pass @admin.register(Bar) class BarAdmin(admin.ModelAdmin): pass ``` 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.

Original source

Related problems