Skip to content

Commit

Permalink
Fixed #33805 -- Made admin's many-to-many widgets do not display help…
Browse files Browse the repository at this point in the history
… text for selecting values when allow_multiple_selected is False.
  • Loading branch information
ankurr0y authored and felixxm committed Jun 28, 2022
1 parent 90d2f9f commit eb7b8f3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
8 changes: 6 additions & 2 deletions django/contrib/admin/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,12 @@ def formfield_for_manytomany(self, db_field, request, **kwargs):
kwargs["queryset"] = queryset

form_field = db_field.formfield(**kwargs)
if isinstance(form_field.widget, SelectMultiple) and not isinstance(
form_field.widget, (CheckboxSelectMultiple, AutocompleteSelectMultiple)
if (
isinstance(form_field.widget, SelectMultiple)
and form_field.widget.allow_multiple_selected
and not isinstance(
form_field.widget, (CheckboxSelectMultiple, AutocompleteSelectMultiple)
)
):
msg = _(
"Hold down “Control”, or “Command” on a Mac, to select more than one."
Expand Down
20 changes: 20 additions & 0 deletions tests/admin_widgets/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,26 @@ class AdvisorAdmin(admin.ModelAdmin):
"Hold down “Control”, or “Command” on a Mac, to select more than one.",
)

def test_m2m_widgets_no_allow_multiple_selected(self):
class NoAllowMultipleSelectedWidget(forms.SelectMultiple):
allow_multiple_selected = False

class AdvisorAdmin(admin.ModelAdmin):
filter_vertical = ["companies"]
formfield_overrides = {
ManyToManyField: {"widget": NoAllowMultipleSelectedWidget},
}

self.assertFormfield(
Advisor,
"companies",
widgets.FilteredSelectMultiple,
filter_vertical=["companies"],
)
ma = AdvisorAdmin(Advisor, admin.site)
f = ma.formfield_for_dbfield(Advisor._meta.get_field("companies"), request=None)
self.assertEqual(f.help_text, "")


@override_settings(ROOT_URLCONF="admin_widgets.urls")
class AdminFormfieldForDBFieldWithRequestTests(TestDataMixin, TestCase):
Expand Down

0 comments on commit eb7b8f3

Please sign in to comment.