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

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

I get "Caught TypeError when rendering: the" int "object is not iterable." https://docs.djangoproject.com/en/dev/ref/models/fields/#field-choices says the select argument takes iterations, 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 message?

I tried converting the list to str using map () but got various errors.

+6
source share
2 answers

... says the select argument takes iterations like list or tuple.

No, he says that he takes iterability from 2 tuples.

Repeatable (for example, list or tuple) 2 tuples to use as a choice for this field.

 birth_day = forms.ChoiceField(choices=((str(x), x) for x in range(1,32))) 
+16
source

You need 2 tuples. Use the built-in zip function for the same 2 tuples

 from django import forms class SignUpForm(forms.Form): birth_day = models.IntegerField(choices=list(zip(range(1, 32), range(1, 32))), unique=True) 

Remember (1.32) will create from 1 to 31!

0
source

Source: https://habr.com/ru/post/900872/


All Articles