2

Very strange issue here. I'm using the django-choices module (v1.3), and defined a set of choices like so:

class BreaktimeChoices(DjangoChoices):
    BREAKTIME_NONE = ChoiceItem(value=datetime.time(0,0), label=_('none'))
    BREAKTIME_15_MIN = ChoiceItem(value=datetime.time(0,15), label=_('15 minutes'))
    BREAKTIME_30_MIN = ChoiceItem(value=datetime.time(0,30), label=_('30 minutes'))
    BREAKTIME_45_MIN = ChoiceItem(value=datetime.time(0,45), label=_('45 minutes'))
    BREAKTIME_1_HOUR = ChoiceItem(value=datetime.time(1,0), label=_('1 hour'))

When I then put it into a form (with a select input) I notice that the labels are correct, but the value for BREAKTIME_NONE is 'none' (rather than the intended 00:00:00). When printing out BreaktimeChoices.choices I get this:

((<django.utils.functional.__proxy__ object at 0x7f995c306f50>, <django.utils.functional.__proxy__ object at 0x7f995c306f50>), 
(datetime.time(0, 15), <django.utils.functional.__proxy__ object at 0x7f995c310f50>),
(datetime.time(0, 30), <django.utils.functional.__proxy__ object at 0x7f995c310bd0>),
(datetime.time(0, 45), <django.utils.functional.__proxy__ object at 0x7f995c310ed0>),
(datetime.time(1, 0), <django.utils.functional.__proxy__ object at 0x7f995c310fd0>))

As you can see, the datetime.time(0,0) has been changed to a __proxy__ object (translatable string). What's more is, the the pointer address to this proxy is the same as the label value for this choice. I use DjangoChoices all over my project and haven't had this issue before. Has anyone else seen anything like it?

benwad
  • 6,414
  • 10
  • 59
  • 93

1 Answers1

0

It turns out this was fixed in a later version - upgrading django-choices to the latest version worked.

The issue was some code that looked like this:

if not self.value:
    self.value = self.label

...and time(0,0) is evaluated to False in that context.

benwad
  • 6,414
  • 10
  • 59
  • 93
  • 1
    Damn, was about to reply :) Anyway, here is the commit that fixed: https://github.com/bigjason/django-choices/commit/0b00f2e85db9376505503af3c3a75f0cd79e3ae7 – Djizeus Oct 23 '15 at 15:35