From e2b49c379a56730b73d79ec05d6edc790ba08cc9 Mon Sep 17 00:00:00 2001 From: Joseph Kocherhans Date: Wed, 28 Mar 2007 04:31:06 +0000 Subject: newforms-admin: Initial implementation of FormSet. git-svn-id: http://code.djangoproject.com/svn/django/branches/newforms-admin@4836 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- tests/regressiontests/forms/tests.py | 152 +++++++++++++++++++++++++++++++++++ 1 file changed, 152 insertions(+) (limited to 'tests') 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() +
  • Choice:
  • +
  • Votes:
  • + + +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() + + +>>> for form in form_set.get_forms(): # print pre-filled forms +... print form.as_ul() +
  • Choice:
  • +
  • Votes:
  • +
  • Choice:
  • +
  • Votes:
  • +
  • Choice:
  • +
  • Votes:
  • + + +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() +
  • Choice:
  • +
  • Votes:
  • +
  • Delete:
  • +
  • Choice:
  • +
  • Votes:
  • +
  • Delete:
  • +
  • Choice:
  • +
  • Votes:
  • +
  • Delete:
  • + +>>> 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() +
  • Choice:
  • +
  • Votes:
  • +
  • Order:
  • +
  • Choice:
  • +
  • Votes:
  • +
  • Order:
  • +
  • Choice:
  • +
  • Votes:
  • +
  • Order:
  • + +>>> 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) -- cgit v1.3