[Solved]-Coerce in django forms

17👍

TypedChoiceField is just like ChoiceField, except ChoiceField always return unicode.

With TypedChoiceField you pass a function that takes one argument and returns the value cast to the type you want. For example, if you want to coerce the value to integer, use:

int_field = forms.TypedChoiceField(choices=SOME_CHOICES, coerce=int)

The field value will always be an integer or fail validation.

Leave a comment