Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Some Django ChoiceFields with the same choice source

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?

like image 962
baldr Avatar asked Apr 28 '26 21:04

baldr


1 Answers

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))
[]
>>>
like image 80
Alasdair Avatar answered May 01 '26 09:05

Alasdair



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!