在DB中保存Django的formwizard form_list最简单的方法? [英] Easiest way to save Django´s formwizard form_list in DB?

查看:129
本文介绍了在DB中保存Django的formwizard form_list最简单的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经从一个模型创建了多个子表单,以在Django中使用FormWizard。



令人困惑的是保存部分。一旦用户完成表单,我想将信息保存在数据库中。这是相当简单的一种形式,我可以在这里说


one_form_from_model.save()




但是当我收到一个form_list时,可以这样做。我读了一些帖子,但对我没有任何意义。



我可以清理数据


form.cleaned_data for form in form_list]


然后通过每个窗体?但是后来我需要知道我在哪一种形式找到正确的领域。没有更简单的方法吗?



感谢您的建议!

解决方案

尝试以下方式:

  instance = MyModel()
for form in form_list:
的字段,在form.cleaned_data.iteritems()中的值:
setattr(实例,字段,值)
instance.save()
/ pre>

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!

解决方案

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

这篇关于在DB中保存Django的formwizard form_list最简单的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆