From c93686c6987df97880cb0edc5535c6d5cbec9e6d Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Sat, 27 Jan 2007 22:06:56 +0000 Subject: [PATCH] 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. git-svn-id: http://code.djangoproject.com/svn/django/trunk@4437 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/newforms/forms.py | 13 +++++++++++-- django/newforms/models.py | 6 +++--- tests/regressiontests/forms/tests.py | 20 ++++++++++++++++++++ 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/django/newforms/forms.py b/django/newforms/forms.py index 1724a5a69f..3536f942e9 100644 --- a/django/newforms/forms.py +++ b/django/newforms/forms.py @@ -26,12 +26,15 @@ class SortedDictFromList(SortedDict): 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 @@ class BaseForm(StrAndUnicode): 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() diff --git a/django/newforms/models.py b/django/newforms/models.py index 819e70d775..326d8f620f 100644 --- a/django/newforms/models.py +++ b/django/newforms/models.py @@ -64,7 +64,7 @@ def form_for_model(model, form=BaseForm, formfield_callback=lambda f: f.formfiel 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 @@ def form_for_instance(instance, form=BaseForm, formfield_callback=lambda f, **kw 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}) diff --git a/tests/regressiontests/forms/tests.py b/tests/regressiontests/forms/tests.py index f60898c179..0726f90d53 100644 --- a/tests/regressiontests/forms/tests.py +++ b/tests/regressiontests/forms/tests.py @@ -2277,6 +2277,8 @@ Form.clean() is required to return a dictionary of all clean data. >>> 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 @@ subclass' __init__(). Last name: Birthday: +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 +Field1: +Field2: +>>> field_list = [('field3', CharField()), ('field4', CharField())] +>>> my_form = MyForm(field_list=field_list) +>>> print my_form +Field3: +Field4: + 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