Django: How to save a formset based on two models

纵饮孤独 提交于 2019-12-04 09:11:04

Check this out: http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#inline-formsets This really simplifies the related by foreign-keys use case which should perfectly fit you.

You shouldn't iterate over the cleaned_data like you do, a simple form.save() should do it. Also, it strikes me as odd that you would iterate over the form rather than the formset in the template. I'm super sleepy atm, so I dunno. And your code does not return a response when the formset isn't valid. You'll get a 500 error for that.

Basically you should use a form for the Balanta model and a inline formset for the other one. Then you can carry out the validation in two parts: if formset is valid... and if form is valid...

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