简体   繁体   English

使用modelformset_factory时,如何在Django中区分表单集?

[英]How to differ formsets in django when using modelformset_factory?

Let's say I have an Contact object and I want to have two groups of contact Formsets in django(1.8) divided by fieldset tag in html template. 假设我有一个Contact对象,并且我想在django(1.8)中有两组Contact Formset,并在html模板中除以fieldset标签。 I use modelformset_factory. 我使用modelformset_factory。 Regardless I use one or two different factory functions, fields in these two formsets have same id in html. 无论我使用一个或两个不同的工厂函数,这两个表单集中的字段在html中都具有相同的id。 Since http.Request.body is dictionary, I lose information about one of the two formsets. 由于http.Request.body是字典,因此我丢失了有关两个表单集之一的信息。

contacts_formset = modelformset_factory(
  models.Contact,
  form=forms.ContactDetailForm,
  extra=2)

contacts_escalation_formset_new = contacts_formset(
    queryset=models.Contact.objects.none())

contacts_other_formset_new = contacts_formset(
    queryset=models.Contact.objects.none())

in HTML: 在HTML中:

input id="id_form-0-name" maxlength="155" name="form-0-name" type="text"
input id="id_form-0-name" maxlength="155" name="form-0-name" type="text"

For simple django form, there is keyword "prefix=..." . 对于简单的django形式,有关键字“ prefix = ...”。 But this factory function does not have this argument. 但是此工厂函数没有此参数。 How can I solve it? 我该如何解决?

The modelformset_factory class returns a FormSet class. modelformset_factory类返回一个formset类。 This FormSet class has a optional prefix argument, similar to Form classes. 该FormSet类具有一个可选的prefix参数,类似于Form类。

contacts_escalation_formset_new = contacts_formset(
    prefix='escalation',
    queryset=models.Contact.objects.none(),
)

contacts_other_formset_new = contacts_formset(
    prefix='other'
    queryset=models.Contact.objects.none(),
)

See the docs on using more than one formset in a view for another example. 有关另一个示例,请参阅有关在视图中使用多个表单集的文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM