Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - ChoiceField cleaned_data gets String instead of Integer

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?

like image 745
Jorge Cámara Avatar asked Dec 01 '17 19:12

Jorge Cámara


1 Answers

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: ')
like image 75
Jorge Cámara Avatar answered Nov 08 '22 21:11

Jorge Cámara