Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed #15127 -- Properly copy the choices of choice fields. Thanks, d…

…ready and Julian Phalip.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16416 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 22529d41b26137ac87c5e08a6c19e6e91552756e 1 parent aa40dc6
@jezdez jezdez authored
View
5 django/forms/fields.py
@@ -657,6 +657,11 @@ def __init__(self, choices=(), required=True, widget=None, label=None,
initial=initial, help_text=help_text, *args, **kwargs)
self.choices = choices
+ def __deepcopy__(self, memo):
+ result = super(ChoiceField, self).__deepcopy__(memo)
+ result._choices = copy.deepcopy(self._choices, memo)
+ return result
+
def _get_choices(self):
return self._choices
View
22 tests/regressiontests/forms/tests/forms.py
@@ -771,6 +771,26 @@ def __init__(self, name_max_length=None, *args, **kwargs):
f = Person(name_max_length=None)
self.assertEqual(f['first_name'].field.max_length, f['last_name'].field.max_length, (30, 30))
+ # Similarly, choices do not persist from one Form instance to the next.
+ # Refs #15127.
+ class Person(Form):
+ first_name = CharField(required=False)
+ last_name = CharField(required=False)
+ gender = ChoiceField(choices=(('f', 'Female'), ('m', 'Male')))
+
+ def __init__(self, allow_unspec_gender=False, *args, **kwargs):
+ super(Person, self).__init__(*args, **kwargs)
+
+ if allow_unspec_gender:
+ self.fields['gender'].choices += (('u', 'Unspecified'),)
+
+ f = Person()
+ self.assertEqual(f['gender'].field.choices, [('f', 'Female'), ('m', 'Male')])
+ f = Person(allow_unspec_gender=True)
+ self.assertEqual(f['gender'].field.choices, [('f', 'Female'), ('m', 'Male'), ('u', 'Unspecified')])
+ f = Person()
+ self.assertEqual(f['gender'].field.choices, [('f', 'Female'), ('m', 'Male')])
+
def test_hidden_widget(self):
# HiddenInput widgets are displayed differently in the as_table(), as_ul())
# and as_p() output of a Form -- their verbose names are not displayed, and a
@@ -1154,7 +1174,7 @@ class UserRegistration(Form):
def test_boundfield_values(self):
# It's possible to get to the value which would be used for rendering
# the widget for a field by using the BoundField's value method.
-
+
class UserRegistration(Form):
username = CharField(max_length=10, initial='djangonaut')
password = CharField(widget=PasswordInput)
Please sign in to comment.
Something went wrong with that request. Please try again.