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
.
The pull request has been made:
https://github.com/django/django/pull/4965