Opened 7 years ago

Closed 7 years ago

#15127 closed Bug (fixed)

form.fields is still tied to class variables

Reported by: Michael Nelson Owned by: nobody
Component: Forms Version: master
Severity: Normal Keywords:
Cc: wil@… Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Karen Tracey)

From django.forms.forms.BaseForm.__init__, it seems the intention is that a form's self.fields should be safe to update

        # The base_fields class attribute is the *class-wide* definition of
        # fields. Because a particular *instance* of the class might want to
        # alter self.fields, we create self.fields here by copying base_fields.
        # Instances should always modify self.fields; they should not modify
        # self.base_fields.
        self.fields = deepcopy(self.base_fields)


In [1]: from django import forms

In [5]: class MyForm(forms.Form):
    title = forms.ChoiceField(choices=(('mr', 'Mr.'), ('mrs', 'Mrs.')))
   ...:     def __init__(self, *args, **kwargs):
   ...:         super(MyForm, self).__init__(*args, **kwargs)
   ...:         self.fields['title'].choices += [('you', 'who')]

In [7]: f1 = MyForm()

In [8]: f1.fields['title'].choices
Out[8]: [('mr', 'Mr.'), ('mrs', 'Mrs.'), ('you', 'who')]

In [9]: f2 = MyForm()

In [10]: f2.fields['title'].choices
Out[10]: [('mr', 'Mr.'), ('mrs', 'Mrs.'), ('you', 'who'), ('you', 'who')]

In [11]: f3 = MyForm()

In [12]: f3.fields['title'].choices
[('mr', 'Mr.'),
 ('mrs', 'Mrs.'),
 ('you', 'who'),
 ('you', 'who'),
 ('you', 'who')]

In [17]: f3.base_fields['title']
Out[17]: <django.forms.fields.ChoiceField object at 0x26cc910>

In [18]: f3.fields['title']
Out[18]: <django.forms.fields.ChoiceField object at 0x26ccb10>

In [19]: f3.base_fields['title'].choices
[('mr', 'Mr.'),
 ('mrs', 'Mrs.'),
 ('you', 'who'),
 ('you', 'who'),
 ('you', 'who')]

So it seems that the instance field is definitely a deep copy, but the attributes of are not. A friend then pointed out Field.__deepcopy__:

which explains why the attributes are shared, but not whether this affect is intentional.

Attachments (2)

choicefield-deepcopy-v1.diff (2.0 KB) - added by Wil Tan 7 years ago.
deepcopy ChoiceField._choices with regression test
15127-form-field-choices-deepcopy.diff (2.5 KB) - added by Julien Phalip 7 years ago.

Download all attachments as: .zip

Change History (9)

comment:1 Changed 7 years ago by Karen Tracey

Description: modified (diff)

comment:2 Changed 7 years ago by Russell Keith-Magee

Triage Stage: UnreviewedAccepted

comment:3 Changed 7 years ago by Łukasz Rekucki

Severity: Normal
Type: Bug

Changed 7 years ago by Wil Tan

deepcopy ChoiceField._choices with regression test

comment:4 Changed 7 years ago by Wil Tan

Cc: wil@… added
Easy pickings: unset
Has patch: set
UI/UX: unset

Could someone review the patch I just attached?

Changed 7 years ago by Julien Phalip

comment:5 Changed 7 years ago by Julien Phalip

Triage Stage: AcceptedReady for checkin

The patch looks good, thanks! I've just made the tests slightly more explicit ;)

comment:6 Changed 7 years ago by Wil Tan

nice, thanks for that :)

comment:7 Changed 7 years ago by Jannis Leidel

Resolution: fixed
Status: newclosed

In [16416]:

Fixed #15127 -- Properly copy the choices of choice fields. Thanks, dready and Julian Phalip.

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