How to save formwizard POST request to a database

36 Views Asked by At

How do I save the form_data to a database?

forms.py

class ContactWizard(SessionWizardView):

        template_name ='fitness/general.html'
        def done(self, form_list, **kwargs):
            form_data = process_form_data(form_list)
            return render_to_response('fitness/general2.html', {'form_data': form_data})



def process_form_data(form_list):
    form_data = [form.cleaned_data for form in form_list]
    return form_data
1

There are 1 best solutions below

0
Meng si On

I have encountered this problem before while doing an online resume builder

def done(self, form_list , **kwargs):
    form_data = [
            form for form in form_list
        ]
    first_form = form_data[0]
    first_instance = first_form.save(commit = False)
    #TODO: add other stuff before saving
    first_instance.save()

I believe this code may help you. It does nothing extra, as form_data contains all the forms, you can get them by indexing. Of course you can use loop but usually different forms may require different procedures.