PictureMeAndYou
PictureMeAndYou

Reputation: 533

Choices must be iterable

I am having an issue with Django and Python

I am facing the error .from_hour: (fields.E005) 'choices' must be an iterable containing (actual value, human readable name) tuples. Can someone help me understand what the error is? I know if I comment from_hour and to_hour it runs

Here is my code

WEEKDAYS = [
  (1, _("Monday")),
  (2, _("Tuesday")),
  (3, _("Wednesday")),
  (4, _("Thursday")),
  (5, _("Friday")),
  (6, _("Saturday")),
  (7, _("Sunday")),
]


weekday_from = models.IntegerField(choices=WEEKDAYS, unique=True)
weekday_to = models.IntegerField(choices=WEEKDAYS)
from_hour = models.IntegerField(choices=range(1,25))
to_hour = models.IntegerField(choices=range(1,25))

def get_weekday_from_display(self):
    return WEEKDAYS[self.weekday_from]

def get_weekday_to_display(self):
    return WEEKDAYS[self.weekday_to]

Upvotes: 10

Views: 16487

Answers (3)

hassanzadeh.sd
hassanzadeh.sd

Reputation: 3471

you must use WEEKDAYS.choices in choices attr, change this line:

weekday_from = models.IntegerField(choices=WEEKDAYS, unique=True)

to

weekday_from = models.IntegerField(choices=WEEKDAYS.choices, unique=True)

Upvotes: 3

mic4ael
mic4ael

Reputation: 8300

The error is related to from_hour. You should provide a collection of tuples like from_hour = models.IntegerField(choices=[(x, str(x)) for x in range(1,25)]). And I think you should do the same for the to_hour field.

Upvotes: 5

Mohammad Hossein Gerami
Mohammad Hossein Gerami

Reputation: 1388

you must set values in ''

STATUS_CHOICES = (
    ('d', 'Draft'),
    ('p', 'Published'),
)

Upvotes: 7

Related Questions