表单集数据初始化

时间:2019-06-14 15:21:41

标签: python django django-forms formset

我试图了解表单集数据的初始化方式。要创建表单集,请使用formset_factory并定义参数“ extra”和“ max_num”,这些参数确定初始/填充表单之后的多余空白表单数以及相应地张贴的最大表单数。但是,当您创建已定义的表单集的实例作为数据传递到模板时,您将向该实例传递更多数据,特别是“ form-TOTAL_FORMS”,“ form-INITIAL_FORMS”和“ form-MAX_NUM_FORMS”。但是我们还没有在formset_factory中定义max_num_forms吗?我们为什么要定义两次?对于文档来说,最初的表格似乎就是听起来的样子-已经填写的表格与“额外”有所不同。只是不理解两次定义最大arg。

1 个答案:

答案 0 :(得分:0)

根据文档:

You may have noticed the additional data (form-TOTAL_FORMS, form-INITIAL_FORMS and form-MAX_NUM_FORMS) that was required in the formset’s data above. This data is required for the ManagementForm. This form is used by the formset to manage the collection of forms contained in the formset. If you don’t provide this management data, an exception will be raised.

https://docs.djangoproject.com/en/2.2/topics/forms/formsets/#understanding-the-managementform