Django admin turned upside down a lot?

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?

+7
python django django-models django-admin
source share
1 answer

You do not provide a link to this requirement:

Django's documentation (perhaps incorrectly) says: "You must tell the Djangos administrator so that he does not display this widget."

therefore, I can only refer to the current (1.10) documentation for Django . He is currently talking about "ManyToMany" in admin :

Django displays an admin widget for the many-to-many field of the model, which defines the relationship (in this case, Group ). If you want to use the built-in model to represent many-to-many relationships, you must tell the Djangos administrator that this widget is not displayed, otherwise you will get two widgets on the admin page to manage this relationship.

So, in response to your correct statement:

But since this is a symmetrical relationship, there is no logical reason why it cannot be controlled with either the Person or the Group (but not both) without changing the database schema.

the reason is that a many-to-many relationship needs to be defined somewhere; You decided to define it on the group model to determine the default administrator behavior. If you want to move it, you will need to migrate the database for this to happen.

If, on the other hand, you want this documented behavior to be different, without changing its use, you do not seem to ask a question that is suitable for StackOverflow. It is better to report a bug with the program in tracking project errors by requesting a change in software behavior.

0
source share

All Articles