选择必须是可迭代的

时间:2015-10-25 06:45:29

标签: python django

我遇到了Django和Python的问题

我遇到错误.from_hour: (fields.E005) 'choices' must be an iterable containing (actual value, human readable name) tuples.有人可以帮我理解错误是什么吗?我知道如果我评论from_hourto_hour它会运行

这是我的代码

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]

3 个答案:

答案 0 :(得分:4)

错误与from_hour有关。您应该提供像from_hour = models.IntegerField(choices=[(x, str(x)) for x in range(1,25)])这样的元组集合。我认为你应该对to_hour字段做同样的事情。

答案 1 :(得分:1)

您必须在''

中设置值
STATUS_CHOICES = (
    ('d', 'Draft'),
    ('p', 'Published'),
)

答案 2 :(得分:0)

您必须在选择属性中使用 WEEKDAYS.choices ,更改此行:

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

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