选择必须是iterab

2024-04-24 18:40:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我对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]

Tags: tofromselfgetreturnmodelsdef错误
2条回答

该错误与from_hour有关。您应该提供一个元组集合,比如from_hour = models.IntegerField(choices=[(x, str(x)) for x in range(1,25)])。我认为您应该对to_hour字段执行同样的操作。

必须在“”中设置值

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

相关问题 更多 >