The answer to the Django admin question ManyToMany inline "does not have a ForeignKey for" error refers to the Django Admin documentation. Presented models:
class Person(models.Model): name = models.CharField(max_length=128) class Group(models.Model): name = models.CharField(max_length=128) members = models.ManyToManyField(Person, related_name='groups')
and the built-in admin classes:
class MembershipInline(admin.TabularInline): model = Group.members.through class PersonAdmin(admin.ModelAdmin): inlines = [MembershipInline,] class GroupAdmin(admin.ModelAdmin): inlines = [MembershipInline,] exclude = ('members',)
... which allows you to manage group membership on the Person page, but not on the Group page. But what if an administrator wants to manage members only from the Group page? Getting rid of the exclude line will allow both pages to manage relationships, but the Django documentation (possibly incorrectly) says: "You have to tell the Djangos administrator to not display this widget." They probably mean that you should "tell the Django administrator not to display it - nothing bad will happen if you do not, but it is redundant."
So, without changing the model, is it possible to exclude the membership widget on the Person page instead of the Group page? Both obvious attempts:
class PersonAdmin(admin.ModelAdmin): inlines = [MembershipInline,] exclude = ('Group.members',)
and
class PersonAdmin(admin.ModelAdmin): inlines = [MembershipInline,] exclude = ('groups',)
(second using related_name from the model) with an error:
'PersonAdmin.exclude' refers to field 'groups' that is missing from the form.
Yes, the model can be modified to place ManyToManyField under Person . But since this is a symmetrical relationship, there is no logical reason why it could not be managed with either the Person or the Group (but not both) without changing the database schema. Can Django Admin manage group membership on the group page and exclude it from the user page?
python django django-models django-admin
Dave
source share