Django 复合/嵌套/子表单?
我正在寻找这些 Django SuperForms 的更新版本。似乎无法让它在 Django 1.2 中工作。特别是,我希望它能够与 ModelForms 一起使用。
我的用例几乎与他的相同;我有一个 Address
模型,我想在各个地方将其用作子表单。尝试将视图函数中的所有内容组合起来是一件痛苦的事情。
I'm looking for an updated version of these Django SuperForms. Can't seem to get it to work in Django 1.2. In particular, I'd like it to work with ModelForms.
My use case is almost identical to his; I have an Address
model that I'd like to use as a sub-form in various places. It's a pain to try and combine everything in the view func.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

我已更新 superforms.py 以使用 1.2,并将其附加到您链接到的票证中: http://code.djangoproject.com/attachment/ticket/3706/superform.2.py
我正在从事的一个项目可以从中受益,所以我想我'我也会花时间帮助你。
请记住,我只是让它在 1.2 中工作,并没有真正尝试清理内部结构。现在我已经有了证明 API 的测试用例,我可以稍后返回并清理它。
如果您将其与 ModelForms 一起使用并且您想要 save() 功能,则必须重写 SuperForm 类上的方法。
我目前在我的“公共”存储库中本地拥有它,代码覆盖率约为 90%,涵盖多个子表单、混合子表单和子表单。声明的表单和模型表单。我已经包含了下面的测试用例(注意它使用我的
TestCaseBase
类,但这应该可以让您了解 API 的要点)。如果您有任何疑问或我遗漏的任何领域,请告诉我。享受吧!
I've updated superforms.py to work w/1.2, and attached it to the ticket you linked to: http://code.djangoproject.com/attachment/ticket/3706/superform.2.py
There's a project I'm working on that could benefit from this, so I figured I'd spend the time and help you out as well.
Keep in mind that I just got this to work w/1.2, and didn't really try to clean up the internals. Now that I have test cases proving the API, I can go back and clean that up later.
If you're using it with ModelForms and you'd like the save() functionality you'll have to override the method on your SuperForm class.
I currently have it locally in my "common" repository w/~90% code coverage that covers multiple SubForms, mixed SubForms & declared forms, and ModelForms. I've included the test cases below (beware it uses my
TestCaseBase
class, but this should give you the gist of the API). Let me know if you have questions, or any areas I missed.Enjoy!