首页 > 解决方案 > 表单集数据初始化

问题描述

我试图了解表单集数据的初始化是如何工作的。要创建一个表单集,您可以使用 formset_factory 并定义参数“extra”和“max_num”,它们确定初始/填充表单之后额外空白表单的数量以及相应发布的表单的最大数量。但是,当您创建已定义表单集的实例以作为数据传递给模板时,您会向该实例传递更多数据,特别是“form-TOTAL_FORMS”、“form-INITIAL_FORMS”和“form-MAX_NUM_FORMS”。但是我们不是已经在 formset_factory 中定义了 max_num_forms 吗?为什么我们要定义两次?对于文档,初始表格似乎就是它听起来的样子——已经填写的表格,这与“额外”不同。只是不明白两次定义最大参数。

标签: pythondjangodjango-formsformset

解决方案


根据文档:

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


推荐阅读