Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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...
commit 70e5dce3651de6f5494233802a5de609182045df 1 parent 813c48e
@malcolmt malcolmt authored
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)
Please sign in to comment.
Something went wrong with that request. Please try again.