[Django] #33731: Updating choices of ChoiceField does not update the underlying widget's choices

9 views
Skip to first unread message

Django

unread,
May 20, 2022, 3:50:31 PM5/20/22
to django-...@googlegroups.com
#33731: Updating choices of ChoiceField does not update the underlying widget's
choices
-------------------------------------+-------------------------------------
Reporter: Stefan- | Owner: nobody
Ionut Tarabuta |
Type: Bug | Status: new
Component: Forms | Version: 3.2
Severity: Normal | Keywords: ChoiceField,
Triage Stage: | choices, form
Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
== Issue

The choices of ChoiceField can be updated by reassigning the choices
attribute to a new list of choices. For example, the code below will
append 2 more options to the existing choices.

{{{
self.fields["my_field"].choices += (("choice1", "Choice 1"), ("choice2",
"Choice 2"))
}}}

Any operation which involves assignment correctly updates the choices of
ChoiceField as well as the underlying widget's choices (what gets rendered
in the browser).

However, using other list operations, such as insert, will not update the
underlying widget's choices. These operations are useful when precise
ordering of choices is desired. For example, the following code will not
update the underlying widget's choices (what gets rendered in the
browser), but just the form field's choices:

{{{
self.fields["my_field"].choices.insert(5, ("choice1", "Choice 1"))
}}}

== Workaround

The issue can be worked around by performing any assignment on the choices
of the ChoiceField, but this is by no means a permanent solution. See the
code below which will cause the widget choices to also be updated.

{{{
self.fields["my_field"].choices.insert(5, ("choice1", "Choice 1")) #
Insert desired choice. Field is updated, but widget is not.

self.fields["my_field"].choices = self.fields["my_field"].choices # Force
the widget to be updated.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/33731>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
May 22, 2022, 6:34:37 AM5/22/22
to django-...@googlegroups.com
#33731: Updating choices of ChoiceField does not update the underlying widget's
choices
-------------------------------------+-------------------------------------
Reporter: Stefan-Ionut | Owner: Aman
Tarabuta | Pandey
Type: Bug | Status: assigned
Component: Forms | Version: 3.2
Severity: Normal | Resolution:

Keywords: ChoiceField, | Triage Stage:
choices, form | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Aman Pandey):

* owner: nobody => Aman Pandey
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/33731#comment:1>

Reply all
Reply to author
Forward
0 new messages