Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #19298 -- Added MultiValueField.__deepcopy__

Thanks nick.phillips at otago.ac.nz for the report.
  • Loading branch information...
commit 4e96dac450e3546bf86220932f5a64fea1ad5bac 1 parent d5d0e03
Tim Graham authored September 10, 2013
5  django/forms/fields.py
@@ -977,6 +977,11 @@ def __init__(self, fields=(), *args, **kwargs):
977 977
                 f.required = False
978 978
         self.fields = fields
979 979
 
  980
+    def __deepcopy__(self, memo):
  981
+        result = super(MultiValueField, self).__deepcopy__(memo)
  982
+        result.fields = tuple([x.__deepcopy__(memo) for x in self.fields])
  983
+        return result
  984
+
980 985
     def validate(self, value):
981 986
         pass
982 987
 
21  tests/forms_tests/tests/test_forms.py
... ...
@@ -1,6 +1,7 @@
1 1
 # -*- coding: utf-8 -*-
2 2
 from __future__ import unicode_literals
3 3
 
  4
+import copy
4 5
 import datetime
5 6
 
6 7
 from django.core.files.uploadedfile import SimpleUploadedFile
@@ -1793,6 +1794,26 @@ class NameForm(Form):
1793 1794
         self.assertTrue(form.is_valid())
1794 1795
         self.assertEqual(form.cleaned_data, {'name' : 'fname lname'})
1795 1796
 
  1797
+    def test_multivalue_deep_copy(self):
  1798
+        """
  1799
+        #19298 -- MultiValueField needs to override the default as it needs
  1800
+        to deep-copy subfields:
  1801
+        """
  1802
+        class ChoicesField(MultiValueField):
  1803
+            def __init__(self, fields=(), *args, **kwargs):
  1804
+                fields = (ChoiceField(label='Rank',
  1805
+                           choices=((1,1),(2,2))),
  1806
+                          CharField(label='Name', max_length=10))
  1807
+                super(ChoicesField, self).__init__(fields=fields, *args, **kwargs)
  1808
+
  1809
+
  1810
+        field = ChoicesField()
  1811
+        field2 = copy.deepcopy(field)
  1812
+        self.assertTrue(isinstance(field2, ChoicesField))
  1813
+        self.assertFalse(id(field2.fields) == id(field.fields))
  1814
+        self.assertFalse(id(field2.fields[0].choices) ==
  1815
+                         id(field.fields[0].choices))
  1816
+
1796 1817
     def test_multivalue_optional_subfields(self):
1797 1818
         class PhoneField(MultiValueField):
1798 1819
             def __init__(self, *args, **kwargs):

0 notes on commit 4e96dac

Please sign in to comment.
Something went wrong with that request. Please try again.