[Django] #27369: Multiple form fields can share the same widget instance

5 views
Skip to first unread message

Django

unread,
Oct 20, 2016, 10:00:44 AM10/20/16
to django-...@googlegroups.com
#27369: Multiple form fields can share the same widget instance
---------------------------------+-----------------------------
Reporter: Michal Petrucha | Owner: Michal Petrucha
Type: Bug | Status: assigned
Component: Forms | Version: master
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+-----------------------------
Say we have a custom form field that defines a certain HTML class on its
widget:
{{{#!python
class CustomChoiceField(ChoiceField):
widget = Select(attrs={'class': 'my-custom-class'})
}}}
If this field type is used for multiple fields in the same form, they will
all share the same widget instance. This leads to conflicts when setting
the fields' choices; since they all use the same widget instance, all
fields will render the same choices from the widget even though the fields
themselves might have different choices. The following test case fails,
for example:
{{{#!python
class TestForm(Form):
field1 = CustomChoiceField(choices=[])
field2 = CustomChoiceField(choices=[])

f = TestForm()
f.fields['field1'].choices = [("1", "1")]
f.fields['field2'].choices = [("2", "2")]
assert f.fields['field1'].widget.choices == [("1", "1")]
assert f.fields['field2'].widget.choices == [("2", "2")]
}}}

I took a deep dive, and found out that since the field instances
themselves share the widget instance (as it's a class attribute that
doesn't need to get instantiated in each field instance), when fields are
getting deepcopied in `BaseForm.__init__`, their copies all share one new
copy of the widget instance.

This is quite easy to fix by deepcopying the widget in the case where it
was specified as an instance, rather than a widget class. Patch coming up
in a couple of minutes.

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

Django

unread,
Oct 20, 2016, 10:08:57 AM10/20/16
to django-...@googlegroups.com
#27369: Multiple form fields can share the same widget instance
-------------------------------------+-------------------------------------

Reporter: Michal Petrucha | Owner: Michal
| Petrucha
Type: Bug | Status: assigned
Component: Forms | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Michal Petrucha):

* needs_better_patch: => 0
* has_patch: 0 => 1
* needs_tests: => 0
* needs_docs: => 0


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

Django

unread,
Oct 22, 2016, 7:04:37 PM10/22/16
to django-...@googlegroups.com
#27369: Multiple form fields can share the same widget instance
-------------------------------------+-------------------------------------

Reporter: Michal Petrucha | Owner: Michal
| Petrucha
Type: Bug | Status: assigned
Component: Forms | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* stage: Unreviewed => Accepted


--
Ticket URL: <https://code.djangoproject.com/ticket/27369#comment:2>

Django

unread,
Oct 25, 2016, 11:02:55 AM10/25/16
to django-...@googlegroups.com
#27369: Multiple form fields can share the same widget instance
-------------------------------------+-------------------------------------

Reporter: Michal Petrucha | Owner: Michal
| Petrucha
Type: Bug | Status: assigned
Component: Forms | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Jaap Roes):

Replying to [comment:1 Michal Petrucha]:
> [https://github.com/django/django/pull/6267 PR]

This links to the wrong PR (#6267), the correct one is this
[https://github.com/django/django/pull/7415 PR #7415]

--
Ticket URL: <https://code.djangoproject.com/ticket/27369#comment:3>

Django

unread,
Oct 26, 2016, 3:31:21 PM10/26/16
to django-...@googlegroups.com
#27369: Multiple form fields can share the same widget instance
-------------------------------------+-------------------------------------

Reporter: Michal Petrucha | Owner: Michal
| Petrucha
Type: Bug | Status: assigned
Component: Forms | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* stage: Accepted => Ready for checkin

Django

unread,
Nov 6, 2016, 1:17:09 AM11/6/16
to django-...@googlegroups.com
#27369: Multiple form fields can share the same widget instance
-------------------------------------+-------------------------------------

Reporter: Michal Petrucha | Owner: Michal
| Petrucha
Type: Bug | Status: closed
Component: Forms | Version: master
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham <timograham@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"09da1e79de5a03a63610822bd4e5fc2adcbeb38a" 09da1e79]:
{{{
#!CommitTicketReference repository=""
revision="09da1e79de5a03a63610822bd4e5fc2adcbeb38a"
Fixed #27369 -- Prevented widgets from being shared between form field
instances.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/27369#comment:4>

Reply all
Reply to author
Forward
0 new messages