Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #3489 -- Changed newforms to use copy.copy() in constructing se…

…lf.fields, so changes to self.fields in a given form instance do not affect other instances

git-svn-id: http://code.djangoproject.com/svn/django/trunk@4504 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 9efa60dafbfa9f23423c6babb8de56e028f24c49 1 parent c61d7e1
Adrian Holovaty authored February 13, 2007
3  django/newforms/forms.py
@@ -7,6 +7,7 @@
7 7
 from fields import Field
8 8
 from widgets import TextInput, Textarea, HiddenInput, MultipleHiddenInput
9 9
 from util import flatatt, StrAndUnicode, ErrorDict, ErrorList, ValidationError
  10
+import copy
10 11
 
11 12
 __all__ = ('BaseForm', 'Form')
12 13
 
@@ -27,7 +28,7 @@ def __init__(self, data=None):
27 28
         dict.__init__(self, dict(data))
28 29
 
29 30
     def copy(self):
30  
-        return SortedDictFromList(self.items())
  31
+        return SortedDictFromList([(k, copy.copy(v)) for k, v in self.items()])
31 32
 
32 33
 class DeclarativeFieldsMetaclass(type):
33 34
     "Metaclass that converts Field attributes to a dictionary called 'base_fields'."
37  tests/regressiontests/forms/tests.py
@@ -2334,6 +2334,43 @@
2334 2334
 <tr><th>Field3:</th><td><input type="text" name="field3" /></td></tr>
2335 2335
 <tr><th>Field4:</th><td><input type="text" name="field4" /></td></tr>
2336 2336
 
  2337
+Similarly, changes to field attributes do not persist from one Form instance
  2338
+to the next.
  2339
+>>> class Person(Form):
  2340
+...     first_name = CharField(required=False)
  2341
+...     last_name = CharField(required=False)
  2342
+...     def __init__(self, names_required=False, *args, **kwargs):
  2343
+...         super(Person, self).__init__(*args, **kwargs)
  2344
+...         if names_required:
  2345
+...             self.fields['first_name'].required = True
  2346
+...             self.fields['last_name'].required = True
  2347
+>>> f = Person(names_required=False)
  2348
+>>> f['first_name'].field.required, f['last_name'].field.required
  2349
+(False, False)
  2350
+>>> f = Person(names_required=True)
  2351
+>>> f['first_name'].field.required, f['last_name'].field.required
  2352
+(True, True)
  2353
+>>> f = Person(names_required=False)
  2354
+>>> f['first_name'].field.required, f['last_name'].field.required
  2355
+(False, False)
  2356
+>>> class Person(Form):
  2357
+...     first_name = CharField(max_length=30)
  2358
+...     last_name = CharField(max_length=30)
  2359
+...     def __init__(self, name_max_length=None, *args, **kwargs):
  2360
+...         super(Person, self).__init__(*args, **kwargs)
  2361
+...         if name_max_length:
  2362
+...             self.fields['first_name'].max_length = name_max_length
  2363
+...             self.fields['last_name'].max_length = name_max_length
  2364
+>>> f = Person(name_max_length=None)
  2365
+>>> f['first_name'].field.max_length, f['last_name'].field.max_length
  2366
+(30, 30)
  2367
+>>> f = Person(name_max_length=20)
  2368
+>>> f['first_name'].field.max_length, f['last_name'].field.max_length
  2369
+(20, 20)
  2370
+>>> f = Person(name_max_length=None)
  2371
+>>> f['first_name'].field.max_length, f['last_name'].field.max_length
  2372
+(30, 30)
  2373
+
2337 2374
 HiddenInput widgets are displayed differently in the as_table(), as_ul()
2338 2375
 and as_p() output of a Form -- their verbose names are not displayed, and a
2339 2376
 separate row is not displayed. They're displayed in the last row of the

0 notes on commit 9efa60d

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