Search code examples
pythondjangopython-2.7django-formwizardchoicefield

Django form-wizard typechoicefield returning string instead of boolean


so i am using Django 1.3 form-wizard https://github.com/stephrdev/django-formwizard and i am running into problems with my typechoice field which is returning u'False' instead of just False (boolean).

What should i do?

ONE_OR_MULTIPLE_CHOICES = (
    (False, 'One'),
    (True, 'Multiple')
)

class PublicJobCreateForm(forms.Form):
    multiple = forms.TypedChoiceField(choices=ONE_OR_MULTIPLE_CHOICES, widget=forms.RadioSelect)

i am calling this way:

def done(self, form_list, **kwargs):
    create_form_data = form_list[0].cleaned_data
    if create_form_data['multiple']:
        print "something"

any ideas?


Solution

  • You haven't specified coerce for your TypedChoiceField.

    ONE_OR_MULTIPLE_CHOICES = (
        (0, 'One'),
        (1, 'Multiple')
    )
    
    multiple = forms.TypedChoiceField(choices=ONE_OR_MULTIPLE_CHOICES,
                                      widget=forms.RadioSelect,
                                      coerce=int)
    

    If you want to use False and True instead of 0 and 1, then note that using coerce=bool does not work. This is because the string 'False' is coerced to True. This answer suggests to use a custom lambda function:

    coerce = lambda x: x == 'True'