[Django]-Django IntegerField with Choice Options (how to create 0-10 integer options)

48👍

Use a Python list comprehension:

CHOICES = [(i,i) for i in range(11)]

This will result in:

[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7), (8, 8), (9, 9), (10,10)]

7👍

As well as @Torsten has mentioned, you could improve it by:

field = models.IntegerField(choices=list(zip(range(1, 10), range(1, 10))), unique=True)

But remember this will gives you from 1 to 9. Put range (1,11) if you want until 10

Leave a comment