#15581: MultipleChoiceInput not validating choices set after field definition 
(with
example)
---------------------------------+---------------------------
 Reporter:  whardier             |         Owner:  nobody
   Status:  new                  |     Milestone:
Component:  Forms                |       Version:  1.3-alpha
 Keywords:  MultipleChoiceField  |  Triage Stage:  Unreviewed
Has patch:  0                    |
---------------------------------+---------------------------
 It appears as though some validation is being done as a Field is
 instantiated.. or the choices are not being migrated to a variable used
 for validation later on per class.  I'm hoping to discover the reason
 behind this not working as expected.

 In the example below I am setting choices for a MultipleChoiceField after
 creating the field instance 'creators':

 {{{
 >>> TestForm = forms.Form(data={u'creators': [u'12', u'7']})
 >>> TestForm.fields['creators'] = forms.MultipleChoiceField()
 >>> TestForm.fields['creators'].widget.choices = [(x.id, unicode(x)) for x
 in User.objects.filter(submission__survey=1).distinct()]
 >>> for choice in TestForm.fields['creators'].widget.choices:
 ...   print choice
 ...
 (6L, u'jeffspencer')
 (1L, u'shanespencer')
 (12L, u'kjohnson')
 (10L, u'plomonaco')
 (7L, u'shanestewart')
 (13L, u'dtollison')
 (11L, u'khaney')
 (16L, u'mbritt')
 (14L, u'roberteoll')
 (9L, u'thomasdunlap')
 (18L, u'jsmith')
 (22L, u'willeagle')
 (15L, u'mlaselle')
 >>> TestForm.is_valid()
 False
 >>> TestForm.errors
 {'creators': [u'Select a valid choice. 12 is not one of the available
 choices.']}
 }}}

 In this working example I am using ModelMultipleChoiceField and a queryset
 to set up the choices of field 'creators' instace immediately.  This works
 the same if I set the queryset later on and use a broader filter for the
 User model initially.

 {{{
 >>> TestForm = forms.Form(data={u'creators': [u'12', u'7']})
 >>> TestForm.fields['creators'] =
 
forms.ModelMultipleChoiceField(queryset=User.objects.filter(submission__survey=1).distinct())
 >>> for choice in TestForm.fields['creators'].widget.choices:
 ...   print choice
 ...
 (6L, u'jeffspencer')
 (1L, u'shanespencer')
 (12L, u'kjohnson')
 (10L, u'plomonaco')
 (7L, u'shanestewart')
 (13L, u'dtollison')
 (11L, u'khaney')
 (16L, u'mbritt')
 (14L, u'roberteoll')
 (9L, u'thomasdunlap')
 (18L, u'jsmith')
 (22L, u'willeagle')
 (15L, u'mlaselle')
 >>> TestForm.is_valid()
 True

 }}}

 In this working example I am using MultipleChoiceField again but setting
 the choices of the 'creators' field instance immediately.

 {{{
 >>> TestForm = forms.Form(data={u'creators': [u'12', u'7']})
 >>> TestForm.fields['creators'] = forms.MultipleChoiceField(choices=[(12,
 'hi'), ('7', 'sup')])
 >>> for choice in TestForm.fields['creators'].widget.choices:
 ...   print choice
 ...
 (12, 'hi')
 ('7', 'sup')
 >>> TestForm.is_valid()
 True
 }}}

 Alternatively setting the choices in afterwords with the previous example
 (removing any query mistakes) gives similar results to the original
 problem.

 {{{
 >>> TestForm = forms.Form(data={u'creators': [u'12', u'7']})
 >>> TestForm.fields['creators'] = forms.MultipleChoiceField()
 >>> TestForm.fields['creators'].widget.choices = [(12, 'hi'), ('7',
 'sup')]
 >>> TestForm.is_valid()
 False
 >>> TestForm.errors
 {'creators': [u'Select a valid choice. 12 is not one of the available
 choices.']}
 }}}

-- 
Ticket URL: <http://code.djangoproject.com/ticket/15581>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to