Permalink
Browse files

Fixed #3489 -- Added proper deepcopying to form fields so that widget…

… instances get copied as well. Patch from Jonathan Buchanan and insin.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@6156 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
1 parent 813c48e commit 70e5dce3651de6f5494233802a5de609182045df @malcolmt malcolmt committed Sep 14, 2007
Showing with 16 additions and 1 deletion.
  1. +7 −0 django/newforms/fields.py
  2. +1 −1 django/newforms/forms.py
  3. +8 −0 tests/regressiontests/forms/tests.py
View
7 django/newforms/fields.py
@@ -2,6 +2,7 @@
Field classes
"""
+import copy
import datetime
import re
import time
@@ -100,6 +101,12 @@ def widget_attrs(self, widget):
"""
return {}
+ def __deepcopy__(self, memo):
+ result = copy.copy(self)
+ memo[id(self)] = result
+ result.widget = copy.deepcopy(self.widget, memo)
+ return result
+
class CharField(Field):
def __init__(self, max_length=None, min_length=None, *args, **kwargs):
self.max_length, self.min_length = max_length, min_length
View
2 django/newforms/forms.py
@@ -31,7 +31,7 @@ def __init__(self, data=None):
dict.__init__(self, dict(data))
def copy(self):
- return SortedDictFromList([(k, copy.copy(v)) for k, v in self.items()])
+ return SortedDictFromList([(k, copy.deepcopy(v)) for k, v in self.items()])
class DeclarativeFieldsMetaclass(type):
"""
View
8 tests/regressiontests/forms/tests.py
@@ -2690,16 +2690,24 @@
... super(Person, self).__init__(*args, **kwargs)
... if names_required:
... self.fields['first_name'].required = True
+... self.fields['first_name'].widget.attrs['class'] = 'required'
... self.fields['last_name'].required = True
+... self.fields['last_name'].widget.attrs['class'] = 'required'
>>> f = Person(names_required=False)
>>> f['first_name'].field.required, f['last_name'].field.required
(False, False)
+>>> f['first_name'].field.widget.attrs, f['last_name'].field.widget.attrs
+({}, {})
>>> f = Person(names_required=True)
>>> f['first_name'].field.required, f['last_name'].field.required
(True, True)
+>>> f['first_name'].field.widget.attrs, f['last_name'].field.widget.attrs
+({'class': 'required'}, {'class': 'required'})
>>> f = Person(names_required=False)
>>> f['first_name'].field.required, f['last_name'].field.required
(False, False)
+>>> f['first_name'].field.widget.attrs, f['last_name'].field.widget.attrs
+({}, {})
>>> class Person(Form):
... first_name = CharField(max_length=30)
... last_name = CharField(max_length=30)

0 comments on commit 70e5dce

Please sign in to comment.