Django

Code

Changeset 4504

Show
Ignore:
Timestamp:
02/13/07 12:04:47 (2 years ago)
Author:
adrian
Message:

Fixed #3489 -- Changed newforms to use copy.copy() in constructing self.fields, so changes to self.fields in a given form instance do not affect other instances

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/newforms/forms.py

    r4440 r4504  
    88from widgets import TextInput, Textarea, HiddenInput, MultipleHiddenInput 
    99from util import flatatt, StrAndUnicode, ErrorDict, ErrorList, ValidationError 
     10import copy 
    1011 
    1112__all__ = ('BaseForm', 'Form') 
     
    2829 
    2930    def copy(self): 
    30         return SortedDictFromList(self.items()
     31        return SortedDictFromList([(k, copy.copy(v)) for k, v in self.items()]
    3132 
    3233class DeclarativeFieldsMetaclass(type): 
  • django/trunk/tests/regressiontests/forms/tests.py

    r4466 r4504  
    23352335<tr><th>Field4:</th><td><input type="text" name="field4" /></td></tr> 
    23362336 
     2337Similarly, changes to field attributes do not persist from one Form instance 
     2338to 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 
    23372374HiddenInput widgets are displayed differently in the as_table(), as_ul() 
    23382375and as_p() output of a Form -- their verbose names are not displayed, and a