Ticket #15127: choicefield-deepcopy-v1.diff

File choicefield-deepcopy-v1.diff, 2.0 KB (added by Wil Tan, 13 years ago)

deepcopy ChoiceField._choices with regression test

  • django/forms/fields.py

     
    695695                    return True
    696696        return False
    697697
     698    def __deepcopy__(self, memo):
     699        result = super(ChoiceField, self).__deepcopy__(memo)
     700        result._choices = copy.copy(self._choices)
     701        return result
     702
     703
    698704class TypedChoiceField(ChoiceField):
    699705    def __init__(self, *args, **kwargs):
    700706        self.coerce = kwargs.pop('coerce', lambda val: val)
  • tests/regressiontests/forms/tests/forms.py

     
    771771        f = Person(name_max_length=None)
    772772        self.assertEqual(f['first_name'].field.max_length, f['last_name'].field.max_length, (30, 30))
    773773
     774        class Person(Form):
     775            first_name = CharField(required=False)
     776            last_name = CharField(required=False)
     777            gender = ChoiceField(choices=(('f', 'Female'), ('m', 'Male')))
     778
     779            def __init__(self, allow_unspec_gender=False, *args, **kwargs):
     780                super(Person, self).__init__(*args, **kwargs)
     781
     782                if allow_unspec_gender:
     783                    self.fields['gender'].choices += (('u', 'Unspecified'),)
     784
     785        f = Person()
     786        self.assertEqual(len(f['gender'].field.choices), 2)
     787        f = Person(allow_unspec_gender=True)
     788        self.assertEqual(len(f['gender'].field.choices), 3)
     789        self.assertEqual(f['gender'].field.choices[2], ('u', 'Unspecified'))
     790        f = Person()
     791        self.assertEqual(len(f['gender'].field.choices), 2)
     792
     793
     794
    774795    def test_hidden_widget(self):
    775796        # HiddenInput widgets are displayed differently in the as_table(), as_ul())
    776797        # and as_p() output of a Form -- their verbose names are not displayed, and a
Back to Top