Author: adrian
Date: 2007-01-27 16:06:56 -0600 (Sat, 27 Jan 2007)
New Revision: 4437

Modified:
   django/trunk/django/newforms/forms.py
   django/trunk/django/newforms/models.py
   django/trunk/tests/regressiontests/forms/tests.py
Log:
Fixed #3334 -- Changed newforms Form class construction so that appending to 
(or altering) self.fields affects only the instance, not the class. As a 
consequence, self.fields is created in Form.__init__(). The form metaclass now 
creates a variable self.base_fields instead of self.fields.

Modified: django/trunk/django/newforms/forms.py
===================================================================
--- django/trunk/django/newforms/forms.py       2007-01-27 21:41:32 UTC (rev 
4436)
+++ django/trunk/django/newforms/forms.py       2007-01-27 22:06:56 UTC (rev 
4437)
@@ -26,12 +26,15 @@
         self.keyOrder = [d[0] for d in data]
         dict.__init__(self, dict(data))
 
+    def copy(self):
+        return SortedDictFromList(self.items())
+
 class DeclarativeFieldsMetaclass(type):
-    "Metaclass that converts Field attributes to a dictionary called 'fields'."
+    "Metaclass that converts Field attributes to a dictionary called 
'base_fields'."
     def __new__(cls, name, bases, attrs):
         fields = [(field_name, attrs.pop(field_name)) for field_name, obj in 
attrs.items() if isinstance(obj, Field)]
         fields.sort(lambda x, y: cmp(x[1].creation_counter, 
y[1].creation_counter))
-        attrs['fields'] = SortedDictFromList(fields)
+        attrs['base_fields'] = SortedDictFromList(fields)
         return type.__new__(cls, name, bases, attrs)
 
 class BaseForm(StrAndUnicode):
@@ -46,6 +49,12 @@
         self.prefix = prefix
         self.initial = initial or {}
         self.__errors = None # Stores the errors after clean() has been called.
+        # The base_fields class attribute is the *class-wide* definition of
+        # fields. Because a particular *instance* of the class might want to
+        # alter self.fields, we create self.fields here by copying base_fields.
+        # Instances should always modify self.fields; they should not modify
+        # self.base_fields.
+        self.fields = self.base_fields.copy()
 
     def __unicode__(self):
         return self.as_table()

Modified: django/trunk/django/newforms/models.py
===================================================================
--- django/trunk/django/newforms/models.py      2007-01-27 21:41:32 UTC (rev 
4436)
+++ django/trunk/django/newforms/models.py      2007-01-27 22:06:56 UTC (rev 
4437)
@@ -64,7 +64,7 @@
         if formfield:
             field_list.append((f.name, formfield))
     fields = SortedDictFromList(field_list)
-    return type(opts.object_name + 'Form', (form,), {'fields': fields, 
'_model': model, 'save': model_save})
+    return type(opts.object_name + 'Form', (form,), {'base_fields': fields, 
'_model': model, 'save': model_save})
 
 def form_for_instance(instance, form=BaseForm, formfield_callback=lambda f, 
**kwargs: f.formfield(**kwargs)):
     """
@@ -87,9 +87,9 @@
             field_list.append((f.name, formfield))
     fields = SortedDictFromList(field_list)
     return type(opts.object_name + 'InstanceForm', (form,),
-        {'fields': fields, '_model': model, 'save': 
make_instance_save(instance)})
+        {'base_fields': fields, '_model': model, 'save': 
make_instance_save(instance)})
 
 def form_for_fields(field_list):
     "Returns a Form class for the given list of Django database field 
instances."
     fields = SortedDictFromList([(f.name, f.formfield()) for f in field_list])
-    return type('FormForFields', (BaseForm,), {'fields': fields})
+    return type('FormForFields', (BaseForm,), {'base_fields': fields})

Modified: django/trunk/tests/regressiontests/forms/tests.py
===================================================================
--- django/trunk/tests/regressiontests/forms/tests.py   2007-01-27 21:41:32 UTC 
(rev 4436)
+++ django/trunk/tests/regressiontests/forms/tests.py   2007-01-27 22:06:56 UTC 
(rev 4437)
@@ -2277,6 +2277,8 @@
 >>> f.clean_data
 {'username': u'adrian', 'password1': u'foo', 'password2': u'foo'}
 
+# Dynamic construction ########################################################
+
 It's possible to construct a Form dynamically by adding to the self.fields
 dictionary in __init__(). Don't forget to call Form.__init__() within the
 subclass' __init__().
@@ -2292,6 +2294,24 @@
 <tr><th>Last name:</th><td><input type="text" name="last_name" /></td></tr>
 <tr><th>Birthday:</th><td><input type="text" name="birthday" /></td></tr>
 
+Instances of a dynamic Form do not persist fields from one Form instance to
+the next.
+>>> class MyForm(Form):
+...     def __init__(self, data=None, auto_id=False, field_list=[]):
+...         Form.__init__(self, data, auto_id)
+...         for field in field_list:
+...             self.fields[field[0]] = field[1]
+>>> field_list = [('field1', CharField()), ('field2', CharField())]
+>>> my_form = MyForm(field_list=field_list)
+>>> print my_form
+<tr><th>Field1:</th><td><input type="text" name="field1" /></td></tr>
+<tr><th>Field2:</th><td><input type="text" name="field2" /></td></tr>
+>>> field_list = [('field3', CharField()), ('field4', CharField())]
+>>> my_form = MyForm(field_list=field_list)
+>>> print my_form
+<tr><th>Field3:</th><td><input type="text" name="field3" /></td></tr>
+<tr><th>Field4:</th><td><input type="text" name="field4" /></td></tr>
+
 HiddenInput widgets are displayed differently in the as_table(), as_ul()
 and as_p() output of a Form -- their verbose names are not displayed, and a
 separate row is not displayed. They're displayed in the last row of the


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to