“ValueError:需要超过0个值才能解包”

2024-05-23 19:51:00 发布

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

我看过这里的问题,但似乎没有一个对我的事业有帮助。本质上我所做的就是调用getAllOpenChoices来尝试返回单选按钮的值,这样当选中一个按钮时,它就会保存。在

表单.py

def getAllOpenChoices():
    listOpenChoice = [('All', 'All'), ('No One', 'No One'), ('Test','Test')]
    all_choices = Requisition.objects.distinct()
    for choices in all_choices:
           temp = (Requisition.objects.filter(open_to=choices))
           listOpenChoice.append(temp)
    return tuple(listOpenChoice)

我得到的这个错误是:

^{pr2}$

正在调用getAllOpenChoices

self.fields['open_to'] = forms.ChoiceField( choices = getAllOpenChoices, widget = forms.RadioSelect())

Tags: tonotestobjectsformsopenall按钮
1条回答
网友
1楼 · 发布于 2024-05-23 19:51:00

选择应该是2元组的列表,比如初始值listOpenChoice

listOpenChoice = [('All', 'All'), ('No One', 'No One'), ('Test','Test')]`

如果扩展该列表,则只应添加2元组。例如:

^{pr2}$

但是,您要附加查询集,例如Requisition.objects.filter(open_to=choices)。这没道理。其中一个查询集是空的,这就是为什么在错误消息“需要超过0个值才能解包”中得到零。在

我不清楚您要在列表中附加什么,所以我不能告诉您如何修复代码。只要只附加2元组,就可以了。在

相关问题 更多 >