Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easiest way to save Django´s formwizard form_list in DB?

I have created multiple sub-forms from one model to use the FormWizard in Django.

Confusing is the saving part. Once the user finished the form, I wanted to save the information in the DB. This is fairly simply with one form, where I can say

one_form_from_model.save()

But could I do it when I get a form_list returned. I read some postings but they did not make any sense to me.

Can I clean the data

form.cleaned_data for form in form_list]

and then go through every form? But then I would need to know in which form I am to find the right fields. Isn´t there any easier way to do it?

Thank you for your advice!

like image 993
Hannes Avatar asked Feb 25 '23 10:02

Hannes


1 Answers

Try something like this:

instance = MyModel()
for form in form_list:
    for field, value in form.cleaned_data.iteritems():
        setattr(instance, field, value)
instance.save()
like image 78
catavaran Avatar answered Apr 08 '23 10:04

catavaran