简体   繁体   English

选择必须是可迭代的

[英]Choices must be iterable

I am having an issue with Django and Python我在使用 Django 和 Python 时遇到问题

I am facing the error .from_hour: (fields.E005) 'choices' must be an iterable containing (actual value, human readable name) tuples.我正面临错误.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我知道如果我评论from_hourto_hour它会运行

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]

you must set values in ''您必须在 '' 中设置值

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

The error is related to from_hour .该错误与from_hour相关。 You should provide a collection of tuples like from_hour = models.IntegerField(choices=[(x, str(x)) for x in range(1,25)]) .您应该提供一组元组,例如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.我认为你应该对to_hour字段做同样的to_hour

you must use WEEKDAYS.choices in choices attr, change this line:您必须在选择属性中使用WEEKDAYS.choices ,更改这一行:

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

to

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在django模型中修复“选择必须是可迭代的”错误? - How to fix 'choices must be an iterable' error in django models? 'choices' 必须是一个包含(实际值,人类可读名称)元组的可迭代对象 - 'choices' must be an iterable containing (actual value, human readable name) tuples 'choices' 必须是一个可迭代的包含(实际值,人类可读的名称) - 'choices' must be an iterable containing (actual value, human readable name) Django 迁移错误:错误:“选择”必须是可迭代的(例如,列表或元组) - Django Migration Error: ERRORS: 'choices' must be an iterable (e.g., a list or tuple) 如何解决 content.Keywords.key_words: (fields.E005) &#39;choices&#39; must be an iterable contains (actual value, human readable name) tuples - How to solve content.Keywords.key_words: (fields.E005) 'choices' must be an iterable containing (actual value, human readable name) tuples TypeError:writerows()参数必须可迭代 - TypeError: writerows() argument must be iterable ValueError:`sequences`必须在Keras中可迭代 - ValueError: `sequences` must be iterable in Keras 从 Django 中的模型类生成可迭代的选择元组 - Generate iterable choices tuple from model class in Django GCP Python SDK 类型错误:值必须是可迭代的 - GCP Python SDK TypeError: Value must be iterable 类型错误:<lambda> * 后的 () 参数必须是可迭代的,而不是 Queue - TypeError: <lambda>() argument after * must be an iterable, not Queue
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM