我想要一个复选框表单,其中可以检查多个选项。为此,我有:
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 }}
答案 0 :(得分:1)
在没有完整回溯的情况下,不确定您收到错误的行。如果你打印choice
..你得到了什么?我会像这样生成choice
:
service_choice = tuple(
[(choice, choice) for choice in
ServiceList.objects.values_list('service_name', flat=True)])