开发者

Django Model Charfield many Choices automatically create?

开发者 https://www.devze.com 2023-03-22 05:59 出处:网络
I want to add some choices between year 1950 to 2020. How can i do that? a = 1950 while a < 2020: b=a + 1

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)]
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号