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
This commit is contained in:
parent
813c48e6eb
commit
70e5dce365
|
@ -2,6 +2,7 @@
|
|||
Field classes
|
||||
"""
|
||||
|
||||
import copy
|
||||
import datetime
|
||||
import re
|
||||
import time
|
||||
|
@ -100,6 +101,12 @@ class Field(object):
|
|||
"""
|
||||
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
|
||||
|
|
|
@ -31,7 +31,7 @@ class SortedDictFromList(SortedDict):
|
|||
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):
|
||||
"""
|
||||
|
|
|
@ -2690,16 +2690,24 @@ to the next.
|
|||
... 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)
|
||||
|
|
Loading…
Reference in New Issue