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:
Malcolm Tredinnick 2007-09-14 03:29:39 +00:00
parent 813c48e6eb
commit 70e5dce365
3 changed files with 16 additions and 1 deletions

View File

@ -2,6 +2,7 @@
Field classes Field classes
""" """
import copy
import datetime import datetime
import re import re
import time import time
@ -100,6 +101,12 @@ class Field(object):
""" """
return {} 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): class CharField(Field):
def __init__(self, max_length=None, min_length=None, *args, **kwargs): def __init__(self, max_length=None, min_length=None, *args, **kwargs):
self.max_length, self.min_length = max_length, min_length self.max_length, self.min_length = max_length, min_length

View File

@ -31,7 +31,7 @@ class SortedDictFromList(SortedDict):
dict.__init__(self, dict(data)) dict.__init__(self, dict(data))
def copy(self): 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): class DeclarativeFieldsMetaclass(type):
""" """

View File

@ -2690,16 +2690,24 @@ to the next.
... super(Person, self).__init__(*args, **kwargs) ... super(Person, self).__init__(*args, **kwargs)
... if names_required: ... if names_required:
... self.fields['first_name'].required = True ... self.fields['first_name'].required = True
... self.fields['first_name'].widget.attrs['class'] = 'required'
... self.fields['last_name'].required = True ... self.fields['last_name'].required = True
... self.fields['last_name'].widget.attrs['class'] = 'required'
>>> f = Person(names_required=False) >>> f = Person(names_required=False)
>>> f['first_name'].field.required, f['last_name'].field.required >>> f['first_name'].field.required, f['last_name'].field.required
(False, False) (False, False)
>>> f['first_name'].field.widget.attrs, f['last_name'].field.widget.attrs
({}, {})
>>> f = Person(names_required=True) >>> f = Person(names_required=True)
>>> f['first_name'].field.required, f['last_name'].field.required >>> f['first_name'].field.required, f['last_name'].field.required
(True, True) (True, True)
>>> f['first_name'].field.widget.attrs, f['last_name'].field.widget.attrs
({'class': 'required'}, {'class': 'required'})
>>> f = Person(names_required=False) >>> f = Person(names_required=False)
>>> f['first_name'].field.required, f['last_name'].field.required >>> f['first_name'].field.required, f['last_name'].field.required
(False, False) (False, False)
>>> f['first_name'].field.widget.attrs, f['last_name'].field.widget.attrs
({}, {})
>>> class Person(Form): >>> class Person(Form):
... first_name = CharField(max_length=30) ... first_name = CharField(max_length=30)
... last_name = CharField(max_length=30) ... last_name = CharField(max_length=30)