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.
ManyToManyField
you're referencingA
. Is that right? I'd expect:ManyToManyField(Foo, related_name='bars')
. – GamecockA
was supposed to beFoo
, will correct – Riddick