/ physicians / 3 / update /“u'form-1-id'”中的MultiValueDictKeyError;保存formset

时间:2016-03-07 16:33:52

标签: django formset

我试图保存我的formset中的所有表单,但它没有通过验证。这是我的代码

def UpdatePhysician(request,id):

physician = get_object_or_404(Physician, id=id)
SpecialtyFormset = modelformset_factory(PhysicianSpecialties, fields=('specialty',),max_num=1, labels=None, )
formset = SpecialtyFormset(queryset=PhysicianSpecialties.objects.filter(employee_academic_degree__employee__pk=physician.employee.id))



if request.method == "POST":
    formset = SpecialtyFormset(request.POST,)
    for form in formset:
        print(form)

    if (formset.is_valid()):

        for form in formset:
            form.save()



        return HttpResponse('yesh')
    else:
        return HttpResponse('nope')

return render(request, 'UpdatePhysician.html', {
                                                'formset': formset,
                                                })
它给我一个像这样的错误

“/ physicianicians / 3 / update /”中的MultiValueDictKeyError “u'form-1-ID' “”

修改

我注意到,当我在formset中只有一个对象时,它可以完美地保存记录

我的模板

{% extends 'prescription_menu.html' %}
{% load bootstrap3 %}
{% load crispy_forms_tags %}
 {% block css %}
    <link rel="stylesheet" type="text/css" href="{{ STATIC_URL   }}site/css/select2.css"/>
{% endblock %}

{% block title %}Physicians{% endblock title %}

{% block container %}
    {% csrf_token %}
<form  method="POST">
    <div class="row">
        <div class="col-md-offset-2 col-md-8">
            {{ formset.management_form }}
            {% for form in formset %}
                {% crispy form %}
            {% endfor %}
            <input type="submit" name="Save" value="Save" class="btn btn-primary" id="submit-id-save">
        </div>
   </div>
</form>
 {% endblock container %}

1 个答案:

答案 0 :(得分:2)

我注意到,当我只保存1条记录时,它通过了验证,所以它不是formset,是帖子中的东西,所以我意识到当每个表单呈现第一个时,遵循布局从形式脆的布局,但是下一个渲染包装在表单标签上,所以我在视图的底部创建了一个特殊的布局类,为我的formset提供布局,如下所示

class SpecialtyFormsetHelper(FormHelper):
def __init__(self, *args, **kwargs):
    super(SpecialtyFormsetHelper, self).__init__(*args, **kwargs)
    self.form_tag = False
    self.render_required_fields = True

,最终代码如下:

def UpdatePhysician(request,id):

physician = get_object_or_404(Physician, id=id)
SpecialtyFormset = modelformset_factory(PhysicianSpecialties, fields=('specialty',),max_num=1,)
formset = SpecialtyFormset(queryset=PhysicianSpecialties.objects.filter(employee_academic_degree__employee__pk=physician.employee.id))
helper = SpecialtyFormsetHelper()
if request.method == "POST":

    formset = SpecialtyFormset(request.POST,)
    for form in formset:
        print(form)

    if formset.is_valid():
        for form in formset:
            form.save()

        return HttpResponse('yesh')
    else:
        return HttpResponse('nope')

return render(request, 'UpdatePhysician.html', {
                                                'formset': formset,
                                                'helper': helper,
                                                })

我只是在模板渲染中添加帮助

像这样:

{{ formset.management_form }}
        {% for form in formset %}
            {% crispy form helper %}
        {% endfor %}

感谢Alasdair的帮助:),您非常乐于助人