summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorJoseph Kocherhans <joseph@jkocherhans.com>2007-03-28 04:31:06 +0000
committerJoseph Kocherhans <joseph@jkocherhans.com>2007-03-28 04:31:06 +0000
commite2b49c379a56730b73d79ec05d6edc790ba08cc9 (patch)
treeceae1d5399ba6d1353765ff76d9b62487c2f3741 /tests
parent132cf258cbdce8bac1d7a3d713c3927f43ead1ed (diff)
newforms-admin: Initial implementation of FormSet.
git-svn-id: http://code.djangoproject.com/svn/django/branches/newforms-admin@4836 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'tests')
-rw-r--r--tests/regressiontests/forms/tests.py152
1 files changed, 152 insertions, 0 deletions
diff --git a/tests/regressiontests/forms/tests.py b/tests/regressiontests/forms/tests.py
index f805a221aa..2e51a45050 100644
--- a/tests/regressiontests/forms/tests.py
+++ b/tests/regressiontests/forms/tests.py
@@ -2895,6 +2895,158 @@ True
>>> p.clean_data
{'first_name': u'John', 'last_name': u'Lennon', 'birthday': datetime.date(1940, 10, 9)}
+# FormSets ####################################################################
+
+FormSets allow you to create a bunch of instances of the same form class and
+get back clean data as a list of dicts.
+
+>>> from django.newforms import formsets
+
+>>> class ChoiceForm(Form):
+... choice = CharField()
+... votes = IntegerField()
+
+
+Create an empty form set
+
+>>> form_set = formsets.FormSet(ChoiceForm, prefix='choices', auto_id=False)
+>>> for form in form_set.get_forms():
+... print form.as_ul()
+<li>Choice: <input type="text" name="choices-0-choice" /></li>
+<li>Votes: <input type="text" name="choices-0-votes" /></li>
+
+
+Forms pre-filled with initial data.
+
+>>> initial_data = [
+... {'votes': 50, 'choice': u'The Doors', 'id': u'0'},
+... {'votes': 51, 'choice': u'The Beatles', 'id': u'1'},
+... ]
+
+>>> form_set = formsets.FormSet(ChoiceForm, initial=initial_data, auto_id=False, prefix='choices')
+>>> print form_set.management_form.as_ul()
+<input type="hidden" name="choices-COUNT" value="3" />
+
+>>> for form in form_set.get_forms(): # print pre-filled forms
+... print form.as_ul()
+<li>Choice: <input type="text" name="choices-0-choice" value="The Doors" /></li>
+<li>Votes: <input type="text" name="choices-0-votes" value="50" /></li>
+<li>Choice: <input type="text" name="choices-1-choice" value="The Beatles" /></li>
+<li>Votes: <input type="text" name="choices-1-votes" value="51" /></li>
+<li>Choice: <input type="text" name="choices-2-choice" /></li>
+<li>Votes: <input type="text" name="choices-2-votes" /></li>
+
+
+Tests for dealing with POSTed data
+
+>>> data = {
+... 'choices-COUNT': u'3', # the number of forms rendered
+... 'choices-0-choice': u'The Doors',
+... 'choices-0-votes': u'50',
+... 'choices-1-choice': u'The Beatles',
+... 'choices-1-votes': u'51',
+... 'choices-2-choice': u'',
+... 'choices-2-votes': u'',
+... }
+
+
+>>> form_set = formsets.FormSet(ChoiceForm, data, auto_id=False, prefix='choices')
+>>> print form_set.is_valid()
+True
+>>> for data in form_set.clean_data:
+... print data
+{'votes': 50, 'choice': u'The Doors'}
+{'votes': 51, 'choice': u'The Beatles'}
+
+
+FormSet with deletion fields
+
+>>> form_set = formsets.FormSetWithDeletion(ChoiceForm, initial=initial_data, auto_id=False, prefix='choices')
+>>> for form in form_set.get_forms(): # print pre-filled forms
+... print form.as_ul()
+<li>Choice: <input type="text" name="choices-0-choice" value="The Doors" /></li>
+<li>Votes: <input type="text" name="choices-0-votes" value="50" /></li>
+<li>Delete: <input type="checkbox" name="choices-0-DELETE" /></li>
+<li>Choice: <input type="text" name="choices-1-choice" value="The Beatles" /></li>
+<li>Votes: <input type="text" name="choices-1-votes" value="51" /></li>
+<li>Delete: <input type="checkbox" name="choices-1-DELETE" /></li>
+<li>Choice: <input type="text" name="choices-2-choice" /></li>
+<li>Votes: <input type="text" name="choices-2-votes" /></li>
+<li>Delete: <input type="checkbox" name="choices-2-DELETE" /></li>
+
+>>> data = {
+... 'choices-COUNT': u'3', # the number of forms rendered
+... 'choices-0-choice': u'Fergie',
+... 'choices-0-votes': u'1000',
+... 'choices-0-DELETE': u'on', # Delete this choice.
+... 'choices-1-choice': u'The Decemberists',
+... 'choices-1-votes': u'150',
+... 'choices-2-choice': u'Calexico',
+... 'choices-2-votes': u'90',
+... }
+
+>>> form_set = formsets.FormSetWithDeletion(ChoiceForm, data, auto_id=False, prefix='choices')
+>>> print form_set.is_valid()
+True
+
+When we access form_set.clean_data, items marked for deletion won't be there,
+but they *will* be in form_set.deleted_data
+
+>>> for data in form_set.clean_data:
+... print data
+{'votes': 150, 'DELETE': False, 'choice': u'The Decemberists'}
+{'votes': 90, 'DELETE': False, 'choice': u'Calexico'}
+
+>>> for data in form_set.deleted_data:
+... print data
+{'votes': 1000, 'DELETE': True, 'choice': u'Fergie'}
+
+
+FormSet with Ordering
+
+>>> form_set = formsets.FormSetWithOrdering(ChoiceForm, initial=initial_data, auto_id=False, prefix='choices')
+>>> for form in form_set.get_forms(): # print pre-filled forms
+... print form.as_ul()
+<li>Choice: <input type="text" name="choices-0-choice" value="The Doors" /></li>
+<li>Votes: <input type="text" name="choices-0-votes" value="50" /></li>
+<li>Order: <input type="text" name="choices-0-ORDER" value="1" /></li>
+<li>Choice: <input type="text" name="choices-1-choice" value="The Beatles" /></li>
+<li>Votes: <input type="text" name="choices-1-votes" value="51" /></li>
+<li>Order: <input type="text" name="choices-1-ORDER" value="2" /></li>
+<li>Choice: <input type="text" name="choices-2-choice" /></li>
+<li>Votes: <input type="text" name="choices-2-votes" /></li>
+<li>Order: <input type="text" name="choices-2-ORDER" value="3" /></li>
+
+>>> data = {
+... 'choices-COUNT': u'4', # the number of forms rendered
+... 'choices-0-choice': u'Fergie',
+... 'choices-0-votes': u'1000',
+... 'choices-0-ORDER': u'3',
+... 'choices-1-choice': u'The Decemberists',
+... 'choices-1-votes': u'150',
+... 'choices-1-ORDER': u'1',
+... 'choices-2-choice': u'Calexico',
+... 'choices-2-votes': u'90',
+... 'choices-2-ORDER': u'2',
+... 'choices-3-choice': u'',
+... 'choices-3-votes': u'',
+... 'choices-3-ORDER': u'4',
+... }
+
+>>> form_set = formsets.FormSetWithOrdering(ChoiceForm, data, auto_id=False, prefix='choices')
+>>> print form_set.is_valid()
+True
+
+The form_set.clean_data will be in the correct order as specified by the
+ORDER field from each form.
+
+>>> for data in form_set.clean_data:
+... print data
+{'votes': 150, 'ORDER': 1, 'choice': u'The Decemberists'}
+{'votes': 90, 'ORDER': 2, 'choice': u'Calexico'}
+{'votes': 1000, 'ORDER': 3, 'choice': u'Fergie'}
+
+
# Forms with NullBooleanFields ################################################
NullBooleanField is a bit of a special case because its presentation (widget)