Opened 2 years ago

Closed 2 years ago

#33805 closed Cleanup/optimization (fixed)

SelectMultiple in ModelAdminForm display help text when allow_multiple_selected is False.

Reported by: Maxim Danilov Owned by: Ankur Roy
Component: contrib.admin Version: 4.1
Severity: Normal Keywords: modeladmin, AdminForm
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

In AdminForm Help text on render for SelectMultiple widget don't check, if widget.allow_multiple_selected = False.

Widget himself on render checks it

# django.forms.widgets rows 684-685
if self.allow_multiple_selected:
   context['widget']['attrs']['multiple'] = True

But help_text for widget, whose is rendered behind widget - don't checks it. There we check only "isinstance"

#  django.contrib.admin.options.py rows 280-281
if (isinstance(form_field.widget, SelectMultiple) ann not isinstance(form_field.widget, (CheckboxSelectMultiple, AutocompleteSelectMultiple))):
    ... # do some stuff with help text

as a result I get "msg", which should not be.

Attachments (1)

Unbenannt.png (3.1 KB ) - added by Maxim Danilov 2 years ago.
rendered m2m field with allow_multiple_selected=False

Download all attachments as: .zip

Change History (7)

by Maxim Danilov, 2 years ago

Attachment: Unbenannt.png added

rendered m2m field with allow_multiple_selected=False

comment:1 by Mariusz Felisiak, 2 years ago

Summary: SelectMultiple in ModelAdminForm help_text renderSelectMultiple in ModelAdminForm display help text when allow_multiple_selected is False.
Triage Stage: UnreviewedAccepted
Type: BugCleanup/optimization

Thanks for the report. As far as I understand correctly, you have a subclass of SelectMultiple with allow_multiple_selected set to False, that's quite niche. However, I agree that we should check allow_multiple_selected in both places:

  • django/contrib/admin/options.py

    diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py
    index a25814b5fb..f959a8dc48 100644
    a b class BaseModelAdmin(metaclass=forms.MediaDefiningClass):  
    314314                kwargs["queryset"] = queryset
    315315
    316316        form_field = db_field.formfield(**kwargs)
    317         if isinstance(form_field.widget, SelectMultiple) and not isinstance(
    318             form_field.widget, (CheckboxSelectMultiple, AutocompleteSelectMultiple)
     317        if (
     318            isinstance(form_field.widget, SelectMultiple)
     319            and form_field.widget.allow_multiple_selected
     320            and not isinstance(
     321                form_field.widget, (CheckboxSelectMultiple, AutocompleteSelectMultiple)
     322            )
    319323        ):
    320324            msg = _(
    321325                "Hold down “Control”, or “Command” on a Mac, to select more than one."

comment:2 by Maxim Danilov, 2 years ago

As far as I understand correctly, you have a subclass of SelectMultiple with allow_multiple_selected set to False.

Exactly.

Last edited 2 years ago by Mariusz Felisiak (previous) (diff)

comment:3 by Ankur Roy, 2 years ago

Owner: changed from nobody to Ankur Roy
Status: newassigned

comment:4 by Mariusz Felisiak, 2 years ago

Has patch: set
Needs tests: set
Patch needs improvement: set

comment:5 by Mariusz Felisiak, 2 years ago

Needs tests: unset
Patch needs improvement: unset
Triage Stage: AcceptedReady for checkin

comment:6 by Mariusz Felisiak <felisiak.mariusz@…>, 2 years ago

Resolution: fixed
Status: assignedclosed

In eb7b8f3:

Fixed #33805 -- Made admin's many-to-many widgets do not display help text for selecting values when allow_multiple_selected is False.

Note: See TracTickets for help on using tickets.
Back to Top