mirror of
https://github.com/django/django.git
synced 2025-10-23 21:59:11 +00:00
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
This commit is contained in:
@@ -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__().
|
||||
<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
|
||||
|
||||
Reference in New Issue
Block a user