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
- [Django]-Django Rest Framework custom authentication
- [Django]-How to select a record and update it, with a single queryset in Django?
- [Django]-Add a count field to a django rest framework serializer
Source:stackexchange.com