Displaying both sides of a ManyToMany relationship in Django admin

旧巷老猫 提交于 2020-03-18 06:05:29

问题


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.



来源:https://stackoverflow.com/questions/43894232/displaying-both-sides-of-a-manytomany-relationship-in-django-admin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!