Django Model Charfield many Choices automatically create?
I want to add some choices between year 1950 to 2020. How can i do that?
a = 1950
while a < 2020:
b=a + 1
YEAR_CHOICES = (
('b', 'b'),
)
class Sezonlar(models.Model):
sezon = models.CharField(max_length开发者_开发技巧=4, choices=YEAR_CHOICES)
The requirement for that choices
field is just:
An iterable (e.g., a list or tuple) of 2-tuples to use as choices for this field.
So you can construct it using a list comprehension like so:
YEAR_CHOICES = [(str(yr), str(yr)) for yr in range(1950, 2020)]
精彩评论