2016-08-02 70 views
1

我已经看过这里的问题,它们都没有帮助我的事业。基本上我正在调用getAllOpenChoices来尝试并返回单选按钮的值,所以当选择一个时,它会保存。“ValueError:需要超过0个值才能解包”

forms.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) 

,我得到这个错误是:

ValueError: need more than 0 values to unpack 

getAllOpenChoices是被称为:

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

回答

0

的选择应该是名单2元组,像你的初始值listOpenChoice

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

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

listOpenChoice.append(('new', 'New')) 

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

我不清楚你想要追加到列表中,所以我不能告诉你如何修复你的代码。只要你只追加2元组,你应该没问题。

+0

完美!谢谢! –

相关问题