2015-04-03 61 views
0

我想要一個複選框的形式,其中可以檢查多個選項。爲此,我有:Django窗體:MultipleChoiceField錯誤太多的值來解壓

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 

回溯:在line 58 錯誤

58 {{ service_form }} 
+0

哪一行會拋出錯誤? – 2015-04-03 20:35:17

回答

1

不確定在沒有完整回溯的情況下哪條線路出現錯誤。如果您打印choice ..你會得到什麼?我會生成choice像這樣:

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