3

I have a model like this:

class MyModel(models.Model):
    DAYS = ((str(d), str(d)) for d in range(1, 29))

    day1 = models.CharField('Day 1', max_length=3, null=True, blank=True, choices=DAYS)
    day2 = models.CharField('Day 2', max_length=3, null=True, blank=True, choices=DAYS)

After it gets rendered I see that only field day1 has choice list, but day2 is empty. If I make another constant DAYS2 with the same definition and assign it to day2 - it works.

It seems Django modifies the source of choices argument? I did not find it in the sources, but maybe I do something wrong? Is it a known limitation?

I can use workaround of applying deepcopy on DAYS. But I do not think it is good style. Is there another way?

baldr
  • 2,891
  • 11
  • 43
  • 61

1 Answers1

3

Use a list for DAYS:

class MyModel(models.Model):
    DAYS = [str(d), str(d)) for d in range(1, 29)]
    day1 = models.CharField('Day 1', max_length=3, null=True, blank=True, choices=DAYS)
    day2 = models.CharField('Day 2', max_length=3, null=True, blank=True, choices=DAYS)

Currently, DAYS is a generator. When you access it the first time to get choices for day1, it returns the results you expect. However, the generator has then been consumed, so you get an empty list when you try to get the choices for day2.

You can see this by trying the following in the Python shell:

>>> DAYS = ((str(d), str(d)) for d in range(1,5))
>>> print(list(DAYS))
[('1', '1'), ('2', '2'), ('3', '3'), ('4', '4')]
>>> print(list(DAYS))
[]
>>>
Alasdair
  • 298,606
  • 55
  • 578
  • 516