From d22290b2ce00909c2f92b6dacadfbfb0deeefd7f Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Sat, 2 May 2009 07:16:30 +0000 Subject: [1.0.X] Fixed #10349 -- Modified ManyToManyFields to allow initial form values to be callables. Thanks to fas for the report and patch. Merge of r10652 from trunk. git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.0.X@10653 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- tests/regressiontests/forms/forms.py | 48 ++++++++++++++++++++++++++++++++---- 1 file changed, 43 insertions(+), 5 deletions(-) (limited to 'tests/regressiontests/forms/forms.py') diff --git a/tests/regressiontests/forms/forms.py b/tests/regressiontests/forms/forms.py index 6bee94e579..4b3746101b 100644 --- a/tests/regressiontests/forms/forms.py +++ b/tests/regressiontests/forms/forms.py @@ -1146,37 +1146,63 @@ possible to specify callable data. >>> class UserRegistration(Form): ... username = CharField(max_length=10) ... password = CharField(widget=PasswordInput) +... options = MultipleChoiceField(choices=[('f','foo'),('b','bar'),('w','whiz')]) We need to define functions that get called later. >>> def initial_django(): ... return 'django' >>> def initial_stephane(): ... return 'stephane' +>>> def initial_options(): +... return ['f','b'] +>>> def initial_other_options(): +... return ['b','w'] + Here, we're not submitting any data, so the initial value will be displayed. ->>> p = UserRegistration(initial={'username': initial_django}, auto_id=False) +>>> p = UserRegistration(initial={'username': initial_django, 'options': initial_options}, auto_id=False) >>> print p.as_ul()
  • Username:
  • Password:
  • +
  • Options:
  • The 'initial' parameter is meaningless if you pass data. ->>> p = UserRegistration({}, initial={'username': initial_django}, auto_id=False) +>>> p = UserRegistration({}, initial={'username': initial_django, 'options': initial_options}, auto_id=False) >>> print p.as_ul()
  • Username:
  • Password:
  • +
  • Options:
  • >>> p = UserRegistration({'username': u''}, initial={'username': initial_django}, auto_id=False) >>> print p.as_ul()
  • Username:
  • Password:
  • ->>> p = UserRegistration({'username': u'foo'}, initial={'username': initial_django}, auto_id=False) +
  • Options:
  • +>>> p = UserRegistration({'username': u'foo', 'options':['f','b']}, initial={'username': initial_django}, auto_id=False) >>> print p.as_ul()
  • Username:
  • Password:
  • +
  • Options:
  • A callable 'initial' value is *not* used as a fallback if data is not provided. In this example, we don't provide a value for 'username', and the form raises a validation error rather than using the initial value for 'username'. ->>> p = UserRegistration({'password': 'secret'}, initial={'username': initial_django}) +>>> p = UserRegistration({'password': 'secret'}, initial={'username': initial_django, 'options': initial_options}) >>> p.errors['username'] [u'This field is required.'] >>> p.is_valid() @@ -1187,14 +1213,26 @@ then the latter will get precedence. >>> class UserRegistration(Form): ... username = CharField(max_length=10, initial=initial_django) ... password = CharField(widget=PasswordInput) +... options = MultipleChoiceField(choices=[('f','foo'),('b','bar'),('w','whiz')], initial=initial_other_options) + >>> p = UserRegistration(auto_id=False) >>> print p.as_ul()
  • Username:
  • Password:
  • ->>> p = UserRegistration(initial={'username': initial_stephane}, auto_id=False) +
  • Options:
  • +>>> p = UserRegistration(initial={'username': initial_stephane, 'options': initial_options}, auto_id=False) >>> print p.as_ul()
  • Username:
  • Password:
  • +
  • Options:
  • # Help text ################################################################### -- cgit v1.3