开发者

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

开发者 https://www.devze.com 2023-02-28 14:43 出处:网络
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 fa

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.sa开发者_如何学Pythonve()

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!


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()
0

精彩评论

暂无评论...
验证码 换一张
取 消