Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Form ChoiceField range(): 'int' object not iterable

from django import forms

class SignUpForm(forms.Form):
    birth_day = forms.ChoiceField(choices=range(1,32))

I'm getting "Caught TypeError while rendering: 'int' object is not iterable". https://docs.djangoproject.com/en/dev/ref/models/fields/#field-choices says the choices argument takes iterables such as a list or tuple.

http://docs.python.org/library/functions.html#range says range() creates a list.

Why am I getting an error?

I tried converting the list to str using map() but received different errors.

like image 231
deadghost Avatar asked Nov 06 '11 05:11

deadghost


1 Answers

... says the choices argument takes iterables such as a list or tuple.

No, it says it takes an iterable of 2-tuples.

An iterable (e.g., a list or tuple) of 2-tuples to use as choices for this field.

birth_day = forms.ChoiceField(choices=((str(x), x) for x in range(1,32)))
like image 174
Ignacio Vazquez-Abrams Avatar answered Sep 30 '22 18:09

Ignacio Vazquez-Abrams