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

夙愿已清 提交于 2019-12-04 15:03:21

问题


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!


回答1:


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


来源:https://stackoverflow.com/questions/5769347/easiest-way-to-save-django%c2%b4s-formwizard-form-list-in-db

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!