Opened 10 years ago

Closed 10 years ago

Last modified 10 years ago

#25085 closed Bug (fixed)

Select Widget's __deepcopy__ does not copy its choices.

Reported by: ericfc Owned by: ericfc
Component: Forms Version: 1.8
Severity: Normal Keywords: Select Widget deepcopy __deepcopy__ choices
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no
Pull Requests:4965 merged, 5025 unmerged

Description (last modified by ericfc)

Select inherits its parent's (Widget's) deepcopy which does not take into consideration its choices field and we get weird behavior like below:

from django import forms 

class Fields(forms.Form):
    field = forms.ChoiceField(choices=(('one', 'one'), ('two', 'two')))

class Foo(Fields):
    def __init__(self, *args, **kwargs):
        super(Foo, self).__init__(*args, **kwargs)
        self.fields['field'].widget.choices[0] = ('foo', 'foo')        

class Bar(Fields):
    pass 

foo = Foo()
bar = Bar()

foo.fields['field'].widget.choices
>>>[('foo', 'foo'), ('two', 'two')]

bar.fields['field'].widget.choices
>>>[('foo', 'foo'), ('two', 'two')]

Where foo and bar have nothing to do with one another.
A more simple example:

import copy
from django import forms

widget = forms.Select()
widget_copy = copy.deepcopy(widget)

widget.attrs is widget_copy.attrs
>>>False
widget.choices is widget_copy.choices
>>>True

Since we did a deepcopy the widget.choices is widget_copy.choices must have returned False yet it returned True.

Change History (8)

comment:1 by ericfc, 10 years ago

Owner: set to ericfc
Status: newassigned

by ericfc, 10 years ago

Attachment: 25085.diff added

comment:2 by ericfc, 10 years ago

The pull request has been made:
https://github.com/django/django/pull/4965

comment:3 by ericfc, 10 years ago

Description: modified (diff)

comment:4 by Tim Graham, 10 years ago

Has patch: set
Triage Stage: UnreviewedAccepted

comment:5 by Tim Graham, 10 years ago

Triage Stage: AcceptedReady for checkin

comment:6 by Tim Graham <timograham@…>, 10 years ago

Resolution: fixed
Status: assignedclosed

In 8ee6a3f:

Fixed #25085 -- Overrode Select widget's deepcopy()

comment:7 by Tim Graham <timograham@…>, 10 years ago

In b356dc4e:

Refs #25085 -- Used more specific assertion in widget test.

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