MultiValueDictKeyError en django modelformset_factory
Estoy tratando de implementar un formset de edición. Luego, estoy creando una instancia de los objetos en el formset usando modelformset_factory. Cuando la solicitud no es POST, el formset se carga perfectamente, pero, si la solicitud es POST, el constructor formset genera un MultiValueDictKeyError.
Este es mi código.
forms.pyclass SchoolSectionForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
self.helper = FormHelper()
self.helper.form_tag = False
self.helper.layout = Layout(
Div(
'name',
css_class='name',
),
)
super(SchoolSectionForm, self).__init__(*args, **kwargs)
class Meta:
model = SchoolSection
exclude = [
'school',
'created_by',
]
class SectionBreakFormSet(BaseFormSet):
def __init__(self, *args, **kwargs):
super(SectionBreakFormSet, self).__init__(*args, **kwargs)
class SectionBreakForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
self.helper = FormHelper()
self.helper.form_tag = False
self.helper.layout = Layout(
Div(
Div(
'start_time',
css_class='start_time',
),
Div(
'end_time',
css_class='end_time',
),
css_class='formset_element',
)
)
super(SectionBreakForm, self).__init__(*args, **kwargs)
class Meta:
model = SectionBreak
exclude = [
'school_section',
'created_by',
]
vistas.pydef school_section_edit(request, section_id):
section = get_object_or_404(
SchoolSection,
id=section_id,
)
current_school = request.user.current_school
school_sections_list = current_school.schoolsection_set.all()
section_break_formset = modelformset_factory(
SectionBreak,
max_num=3,
extra=0,
form=SectionBreakForm,
)
formset_qset = SectionBreak.objects.filter(school_section=section)
formset = section_break_formset(queryset=formset_qset)
school_section_form = SchoolSectionForm(instance=section)
if request.method == 'POST':
school_section_form = SchoolSectionForm(request.POST)
# Bug raises in this line
formset = section_break_formset(request.POST, queryset=formset_qset)
# Bug raises in this line
if school_section_form.is_valid() and formset.is_valid():
school_section_form.save()
formset.save()
messages.success(
request,
u'xxx',
)
return HttpResponseRedirect(reverse('school:school_section_add'))
else:
messages.error(
request,
u'xxx',
)
return render(request, 'school/schoolsection_add.html', {
'school_section_form': school_section_form,
'formset': formset,
'school_sections_list': school_sections_list,
})
modelo<form class="new_section_form" method="post" action="">
<div class="school_section_form">
{% crispy school_section_form %}
</div>
<h3>Horarios de descanso:</h3>
<div class="section_break_formset">
{% crispy formset formset.form.helper %}
</div>
<button class="button color">guardar</button>
</form>
Cuando publico el formulario ... crashh .... tengo este error
gracias por la ayuda...
Tipo de excepción: MultiValueDictKeyError en / administrador / ciclo-educativo / editar / 34 / Valor de excepción: "La clave u'form-0-id 'no se encuentra en <> QueryDict: {u'name': [u'Primaria '], u 'form-MAX_NUM_FORMS': [u'3 '], u'form-TOTAL_FORMS': [u'1 '], u'form-0-start_time': [u'07: 00: 00 '], u'form -0-final-hora-en--l--l-P-6-j-C-1-j-C-1-j-C-1-j-C-1-j-C-1-j-C-1-j-C-1-j-C-1-j-C-1 "