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):
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.
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 .
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')
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.
This code below can display both Foo
and Bar
models in many-to-many relationship on the same page in Django Admin:
class BarInline(admin.TabularInline):
model = Bar.foos.through
@admin.register(Foo)
class FooAdmin(admin.ModelAdmin):
inlines = (BarInline,)
Be careful, if using model = Bar
instead of model = Bar.foos.through
in many-to-many relationship as shown below:
class BarInline(admin.TabularInline):
# model = Bar.foos.through
model = Bar # This will get error
@admin.register(Foo)
class FooAdmin(admin.ModelAdmin):
inlines = (BarInline,)
You will get the error below:
ERRORS: <class 'xxx.admin.BarInline'>: (admin.E202) 'xxx.Bar' has no ForeignKey to 'xxx.Foo'.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.