Displaying both sides of a ManyToMany relationship

2020-07-11 04:54发布

问题:

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):
    """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.

回答1:

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.



回答2:

You can define custom fields for list_display like this:

@admin.register(Foo)
class FooAdmin(admin.ModelAdmin):
    """Foo admin."""

    list_display = ('name', 'get_bars')
    search_fields = ('name',)

    def get_bars(self, obj):
        return obj.bars.all()

This is a very simple example, but I hope it can help you as a starting point.

EDIT:

You can display the associated objects in the edit form as readonly:

readonly_fields = ('get_bars',)
fields = ('name', 'get_bars')


回答3:

There is a module called django-admin-extend which provides a generic mechanism to define "Bidirectional many to many fields". I'm not sure if it still works, because the last contribution is two years old, bit it should be worth giving it a try.