Django表单:MultipleChoiceField错误太多值无法解压缩

时间:2015-04-03 19:25:29

标签: django python-2.7 django-forms

我想要一个复选框表单,其中可以检查多个选项。为此,我有:

forms.py

class ServiceForm(forms.Form):
services = ServiceList.objects.all().values('service_name')

choice = []
for x in services:
    choice.extend(x.values())

service_id = forms.MultipleChoiceField(required=True,
                                       widget=forms.CheckboxSelectMultiple, choices=choice)

views.py

def save_data_into_temp(request):

if request.method == 'POST':
    detail_form = ServiceForm(data=request.POST)
    if detail_form.is_valid():

        detail = detail_form.save(commit=False)

        detail.save()

但是这给了我一个错误

too many values to unpack

回溯: 第58行的错误

58  {{ service_form }}

1 个答案:

答案 0 :(得分:1)

在没有完整回溯的情况下,不确定您收到错误的行。如果你打印choice ..你得到了什么?我会像这样生成choice

service_choice = tuple(
    [(choice, choice) for choice in 
        ServiceList.objects.values_list('service_name', flat=True)])