Django和ModelForm上的fieldsets [英] Django and fieldsets on ModelForm
问题描述
我知道您可以在django中为Admin helpers指定fieldsets。但是,我找不到任何对ModelForms有用的东西。只是一些我不能使用的补丁。我错过了什么吗?有没有办法我可以实现像fieldets这样的东西,而不需要在适当的标签中手动写出我模板中的每个字段。
I know you can specify fieldsets in django for Admin helpers. However, I cannot find anything useful for ModelForms. Just some patches which I cannot use. Am I missing something? Is there a way I could achieve something like fieldsets without manually writing out each field on my template in the appropriate tag.
我最好想遍历一组BoundField 。但是,在ModelForm的末尾做这样的事情:
I would ideally like to iterate through a set of BoundFields. However, doing something like this at the end of my ModelForm:
fieldsets = []
fieldsets.append(('Personal Information',
[username,password,password2,first_name,last_name,email]),) # add a 2 element tuple of string and list of fields
fieldsets.append(('Terms & Conditions',
[acceptterms,acceptprivacy]),) # add a 2 element tuple of string and list of fields
失败,因为我的数据结构中包含的项目是原始字段,而不是BoundField。看起来像BoundFields在飞行中生成...这让我很伤心。我可以创建我自己的forms.Form的子类,它包含一个fieldets的概念(即使是一个粗略的,不是向后兼容的...这只是我自己的项目),如果是,你能给任何指针吗?我不想混淆django代码。
fails as the items contained in my data structure are the raw fields, not the BoundFields. t looks like BoundFields are generated on the fly... this makes me sad. Could I create my own subclass of forms.Form which contains a concept of fieldsets (even a rough one that is not backward compatible... this is just for my own project) and if so, can you give any pointer? I do not want to mess with the django code.
推荐答案
模型中的字段仍处于设计阶段。在Django trac中有一个门票。
Fieldsets in modelforms are still in "design" stage. There's a ticket in Django trac with low activity.
这是我在不久的将来对自己进行研究的一些兴趣,但由于我还没有做到,我可以提供的最好的是这些片段:
It's something I've been interested in researching myself in the near future, but since I haven't done it yet the best I can offer are these snippets:
- Form splitting/Fieldset templatetag
- Sectioned Form
- Forms splitted in fieldsets
编辑:我刚刚注意到这个问题,我意识到需要编辑才能指出卡尔的项目 django-form-utils ,其中包含一个可以包含fieldsets的BetterForm类。如果你喜欢这个项目,给他一个+1的答案在下面:)
I just noticed this question again and I realize it needs an edit to point out Carl's project django-form-utils which contains a BetterForm class which can contain fieldsets. If you like this project give him a +1 for his answer below :)
这篇关于Django和ModelForm上的fieldsets的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!