[英]Django formset: how to manually add management data to request.POST?
I have use-cases, when a formset does not get rendered in the page. 当表单集未在页面中呈现时,我有用例。 In this case
request.POST
does not contain management data. 在这种情况下,
request.POST
不包含管理数据。
How can I manually add missing management data? 如何手动添加丢失的管理数据?
request.POST
QueryDict is read only. request.POST
QueryDict是只读的。
As the answer suggests, I used: 如答案所示,我使用了:
request_post_copy = request.POST.copy()
if not request_post_copy.has_key('form-TOTAL_FORMS'):
request_post_copy.update({
'form-TOTAL_FORMS': 0,
'form-INITIAL_FORMS': 0,
'form-MAX_NUM_FORMS': 1000,
})
If the error is that management form data is missing, then management form data IS missing. 如果错误是缺少管理表单数据,则缺少管理表单数据。
Take a look at this: https://docs.djangoproject.com/en/dev/topics/forms/formsets/#using-a-formset-in-views-and-templates 看看这个: https : //docs.djangoproject.com/en/dev/topics/forms/formsets/#using-a-formset-in-views-and-templates
In the example they use this to display additional fields: 在示例中,他们使用它来显示其他字段:
{{ formset.management_form }}
And if those fields are missing then you get the error you are seeing. 如果缺少这些字段,那么您会看到错误。
Edit: 编辑:
If you want to add management fields manually to data, then you need to add 3 key/value pairs for fields like : 如果要手动向数据添加管理字段,则需要为以下字段添加3个键/值对:
<input type="hidden" name="form-TOTAL_FORMS" value="3" id="id_form-TOTAL_FORMS" />
<input type="hidden" name="form-INITIAL_FORMS" value="3" id="id_form-INITIAL_FORMS" />
<input type="hidden" name="form-MAX_NUM_FORMS" id="id_form-MAX_NUM_FORMS" />
Do something like this: 做这样的事情:
my_post_dict = request.POST.copy()
my_post_dict['form-TOTAL_FORMS'] = value
my_post_dict['form-INITIAL_FORMS'] = value
my_post_dict['form-MAX_NUM_FORMS'] = value
myformset = MyFormSet(my_post_dict)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.