From 05420093ae9024aa94c773de6d6055ec2465d38b Mon Sep 17 00:00:00 2001 From: Adrian Holovaty <adrian@holovaty.com> Date: Wed, 15 Nov 2006 23:17:00 +0000 Subject: [PATCH] newforms: Added unit test showing it's possible to construct the fields in a Form dynamically git-svn-id: http://code.djangoproject.com/svn/django/trunk@4074 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- tests/regressiontests/forms/tests.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/tests/regressiontests/forms/tests.py b/tests/regressiontests/forms/tests.py index 21bd378f1f..e63ca097f7 100644 --- a/tests/regressiontests/forms/tests.py +++ b/tests/regressiontests/forms/tests.py @@ -1194,8 +1194,22 @@ Form.clean() still needs to return a dictionary of all clean data: >>> f.clean() {'username': u'adrian', 'password1': u'foo', 'password2': u'foo'} - - +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__(). +>>> class Person(Form): +... first_name = CharField() +... last_name = CharField() +... def __init__(self): +... super(Person, self).__init__() +... self.fields['birthday'] = DateField() +>>> p = Person() +>>> print p +<table> +<tr><td>First name:</td><td><input type="text" name="first_name" /></td></tr> +<tr><td>Last name:</td><td><input type="text" name="last_name" /></td></tr> +<tr><td>Birthday:</td><td><input type="text" name="birthday" /></td></tr> +</table> """ if __name__ == "__main__":