Editing Django Formsets

390 views Asked by At

I am very new to Django with limited Python experience. I am trying to edit a Form and a Formset. I can edit and save the Form, however I can't get the edits in the Formset to save. Here is my code from Views:

@login_required
def edit_asr(request, asr_id):
    context = RequestContext(request)

    if not is_editor(request):
        return HttpResponse("You are not authorized to view this record")

    if request.method == 'POST':

        asr_record = ASR.objects.get(id=asr_id)
        form = BuildASRForm(request.POST, instance=asr_record)
    formset = ASRTripLegFormset(request.POST, instance=asr_record)

    if form.is_valid():
            form.save(commit=True)
            logger.info("ASR %s was edited by %s" % (asr_id, request.user))


            return HttpResponseRedirect('/asr/%s/' % asr_id)

        else:
            logger.error("Edit ASR form returned the following errors for user %s: %s" % (request.user, form.errors))


    else:
        asr_record = ASR.objects.get(id=asr_id)
        form = BuildASRForm(instance=asr_record)
    formset = ASRTripLegFormset(instance=asr_record)
    return render_to_response('asr/edit_asr.html', {'form': form, 'formset': formset, 'asr_id':asr_id}, context)

I've tried if formset.is_valid(): calls indented under the if form-is_valid(): statement and it throws the MultiValueDictKeyError error. Sorry if this is a novice question, I'm stuck.

Thanks!

0

There are 0 answers