Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving Django FormWizard

Tags:

forms

django

save

I have been struggling to create a django FormWizard. I think that I am pretty close, but I cannot figure out how to save to the database.

I tried the solution suggeted here:

def done(self, form_list, **kwargs):
    instance = MyModel()
    for form in form_list:
        for field, value in form.cleaned_data.iteritems():
            setattr(instance, field, value)
    instance.save()

    return render_to_response('wizard-done.html', {
        'form_data': [form.cleaned_data for form in form_list],
    })

but placing it in the done method results in a No Exception Supplied error. Placing this code in the save method, on the other hand, does not save the information.

I also tried the solution suggested here:

def done(self, form_list, **kwargs):
    for form in form_list:
        form.save()
    return render_to_response('wizard-done.html', {
        'form_data': [form.cleaned_data for form in form_list],
    })

But this returns another error: AttributeError at /wizard/ 'StepOneForm' object has no attribute 'save'. Have you faced this problem? How do I save information to the database after the wizard is submitted? Thanks

like image 718
Nick B Avatar asked Nov 13 '22 11:11

Nick B


1 Answers

def done(self, form_list, **kwargs):
    new = MyModel()
    for form in form_list:
        new = construct_instance(form, new, form._meta.fields, form._meta.exclude)
    new.save()
    return redirect('/')
like image 144
catherine Avatar answered Nov 15 '22 07:11

catherine