Ticket #19298: deepcopy_multivaluefields.patch

File deepcopy_multivaluefields.patch, 1.9 KB (added by anonymous, 2 years ago)
  • django/forms/fields.py

    diff --git a/django/forms/fields.py b/django/forms/fields.py
    index 8abb1b3..3c030df 100644
    a b class MultiValueField(Field): 
    971971                f.required = False
    972972        self.fields = fields
    973973
     974    def __deepcopy__(self, memo):
     975        result = super(MultiValueField, self).__deepcopy__(memo)
     976        result.fields = tuple([x.__deepcopy__(memo) for x in self.fields])
     977        return result
     978
    974979    def validate(self, value):
    975980        pass
    976981
  • tests/forms_tests/tests/test_forms.py

    diff --git a/tests/forms_tests/tests/test_forms.py b/tests/forms_tests/tests/test_forms.py
    index 168bc1d..18ab38f 100644
    a b  
    22from __future__ import unicode_literals
    33
    44import datetime
     5import copy
    56
    67from django.core.files.uploadedfile import SimpleUploadedFile
    78from django.core.validators import RegexValidator
    class FormsTestCase(TestCase): 
    17931794        self.assertTrue(form.is_valid())
    17941795        self.assertEqual(form.cleaned_data, {'name' : 'fname lname'})
    17951796
     1797    def test_deep_copy(self):
     1798        class ChoicesField(MultiValueField):
     1799            def __init__(self, fields=(), *args, **kwargs):
     1800                fields = (ChoiceField(label='Rank',
     1801                           choices=((1,1),(2,2))),
     1802                          CharField(label='Name', max_length=10))
     1803                super(ChoicesField, self).__init__(fields=fields, *args, **kwargs)
     1804
     1805
     1806        field = ChoicesField()
     1807        field2 = copy.deepcopy(field)
     1808        self.assertTrue(isinstance(field2, ChoicesField))
     1809        self.assertFalse(id(field2.fields) == id(field.fields))
     1810        self.assertFalse(id(field2.fields[0].choices) ==
     1811                         id(field.fields[0].choices))
     1812
    17961813    def test_multivalue_optional_subfields(self):
    17971814        class PhoneField(MultiValueField):
    17981815            def __init__(self, *args, **kwargs):
Back to Top