[Django] #35878: Allow to add M2M also in related-model form in admin site

9 views
Skip to first unread message

Django

unread,
Oct 30, 2024, 11:45:51 AM10/30/24
to django-...@googlegroups.com
#35878: Allow to add M2M also in related-model form in admin site
-----------------------+-----------------------------------------
Reporter: David | Type: Uncategorized
Status: new | Component: contrib.admin
Version: | Severity: Normal
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------+-----------------------------------------
Given the following definition of models:

{{{#!python
from django.db import models

class ModelA(models.Model):
name = models.CharField(max_length=10)

class ModelB(models.Model):
name = models.CharField(max_length=10)
others = models.ManyToManyField(ModelA)
}}}

The admin site allows to add the field `ModelB.others` to that model's
admin, but there is no easy way to add the same relation on `ModelA` admin
with the same functionality .

It is possible to add the field by providing a custom form in which the
related-field is defined manually, however the form field widget will not
be wrapped in `RelatedFieldWidgetWrapper` thus the "add new entry" button
will not be present. This can be forced in the `ModelAdmin.form` by
turning the `form` attribute into a property which builds the form class
providing the right parameters to the reverse-field.

It would be a great enhancement to allow for an easier way to add reverse-
m2m to a model-admin or to write down a guide to obtain such result.
--
Ticket URL: <https://code.djangoproject.com/ticket/35878>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Oct 30, 2024, 12:48:16 PM10/30/24
to django-...@googlegroups.com
#35878: Allow to add M2M also in related-model form in admin site
-------------------------------+--------------------------------------
Reporter: David | Owner: (none)
Type: Uncategorized | Status: new
Component: contrib.admin | Version:
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Comment (by David):

To make it work my "workaround" is

{{{#!python
from django import forms
from django.contrib import admin
from django.contrib.admin import widgets
from .models import ModelA, ModelB

class ModelAForm(forms.ModelForm):
others_set = forms.ModelMultipleChoiceField(
queryset=ModelB.objects.all(),
required=False,
widget=widgets.FilteredSelectMultiple(verbose_name='ModelB',
is_stacked=False)
)
def _save_m2m(self):
super()._save_m2m()
self.instance.others_set.set(self.cleaned_data['others_set'],
clear=True)

@admin.register(ModelA)
class ModelAAdmin(admin.ModelAdmin):

@property
def form(self):
class AdminM2MAdminForm(ModelAForm):
"""Helper to display the right widget in admin"""

AdminM2MAdminForm.declared_fields['others_set'].widget =
widgets.RelatedFieldWidgetWrapper(
AdminM2MAdminForm.declared_fields['others_set'].widget,
ModelB.others.through._meta.get_field('modelb').remote_field,
self.admin_site,
)
return AdminM2MAdminForm
}}}

This way the admin displays the "+" button which points to ModelB form in
ModelA admin. Not trivial and quite fragile (some APIs are not
documented).
--
Ticket URL: <https://code.djangoproject.com/ticket/35878#comment:1>

Django

unread,
Oct 30, 2024, 12:56:54 PM10/30/24
to django-...@googlegroups.com
#35878: Allow to add M2M also in related-model form in admin site
-------------------------------+--------------------------------------
Reporter: David | Owner: (none)
Type: Uncategorized | Status: closed
Component: contrib.admin | Version:
Severity: Normal | Resolution: duplicate
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by Sarah Boyce):

* resolution: => duplicate
* status: new => closed

Comment:

I believe this is a duplicate of #897
--
Ticket URL: <https://code.djangoproject.com/ticket/35878#comment:2>
Reply all
Reply to author
Forward
0 new messages