From 203f93a7aa9fbe04fa660c262ed2e730de437faf Mon Sep 17 00:00:00 2001 From: Joseph Kocherhans Date: Sat, 7 Apr 2007 05:56:42 +0000 Subject: newforms-admin: Rewrote most of FormSets and associated tests. They now handle an arbitrary number of extra forms, and deletion and ordering at the same time. BaseFormSet still needs some cleanup, but the tests pass. git-svn-id: http://code.djangoproject.com/svn/django/branches/newforms-admin@4953 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- tests/regressiontests/forms/tests.py | 154 +---------------------------------- 1 file changed, 2 insertions(+), 152 deletions(-) (limited to 'tests/regressiontests/forms/tests.py') diff --git a/tests/regressiontests/forms/tests.py b/tests/regressiontests/forms/tests.py index 839b8fc5d9..afe8b204c6 100644 --- a/tests/regressiontests/forms/tests.py +++ b/tests/regressiontests/forms/tests.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- from localflavor import localflavor_tests from regressions import regression_tests +from formsets import formset_tests form_tests = r""" >>> from django.newforms import * @@ -2898,158 +2899,6 @@ 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) @@ -3451,6 +3300,7 @@ __test__ = { 'form_tests': form_tests, 'localflavor': localflavor_tests, 'regressions': regression_tests, + 'formset_tests': formset_tests, } if __name__ == "__main__": -- cgit v1.3