Django - ChoiceField cleaned_data gets String instead of Integer
Asked Answered
R

1

9

I have a form field called 'units' like this:

    units = forms.ChoiceField(choices=[(x, x) for x in range(1, 11)], help_text = 'Units: ')

When I do form.cleaned_data['units'] I get a String instead of an Integer. How can I change the field to get the Integer?

Recurrent answered 1/12, 2017 at 19:17 Comment(0)
R
20

I finally found the Field type TypedChoiceField , that will return Integer if coerced = Int.

    units = forms.TypedChoiceField(choices=[(x, x) for x in range(1, 11)], coerce=int, help_text = 'Units: ')
Recurrent answered 2/12, 2017 at 10:7 Comment(1)
Thank you! Worked perfectly for me :)Sri

© 2022 - 2024 — McMap. All rights reserved.