【发布时间】:2018-08-20 04:52:33
【问题描述】:
我有 2 个基于 2 个不同模型的表单集。
在调试我的表单集未能通过 is_valid 验证的验证问题时(请参阅下面的错误):
(Pdb) FormsetItem.errors
[{'id': ['Select a valid choice. That choice is not one of the available choices.']}, {'id': ['Select a valid choice. That choice is not one of the available choices.']}, {'id': ['Select a valid choice. That choice is not one of the available choices.']}, {'id': ['Select a valid choice. That choice is not one of the available choices.']}, {'id': ['Select a valid choice. That choice is not one of the available choices.']}, {'id': ['Select a valid choice. That choice is not one of the available choices.']}, {'id': ['Select a valid choice. That choice is not one of the available choices.']}, {'id': ['Select a valid choice. That choice is not one of the available choices.']}, {'id': ['Select a valid choice. That choice is not one of the available choices.']}, {'id': ['Select a valid choice. That choice is not one of the available choices.']}, {'id': ['Select a valid choice. That choice is not one of the available choices.']}, {'id': ['Select a valid choice. That choice is not one of the available choices.']}, {'id': ['Select a valid choice. That choice is not one of the available choices.']}, {'id': ['Select a valid choice. That choice is not one of the available choices.']}, {'id': ['Select a valid choice. That choice is not one of the available choices.']}, {'id': ['Select a valid choice. That choice is not one of the available choices.']}, {'id': ['Select a valid choice. That choice is not one of the available choices.']}, {'id': ['Select a valid choice. That choice is not one of the available choices.']}, {'id': ['Select a valid choice. That choice is not one of the available choices.']}, {'id': ['Select a valid choice. That choice is not one of the available choices.']}, {'id': ['Select a valid choice. That choice is not one of the available choices.']}, {'id': ['Select a valid choice. That choice is not one of the available choices.']}, {'id': ['Select a valid choice. That choice is not one of the available choices.']}, {'id': ['Select a valid choice. That choice is not one of the available choices.']}, {'id': ['Select a valid choice. That choice is not one of the available choices.']}, {'id': ['Select a valid choice. That choice is not one of the available choices.']}]
我注意到,当我为 2 个表单集呈现管理表单时:
{{ FormsetItem.management_form }}
{{ FormsetCat.management_form }}
它为两者创建完全相同的 HTML:
<input type="hidden" name="form-TOTAL_FORMS" value="26" id="id_form-TOTAL_FORMS" /><input type="hidden" name="form-INITIAL_FORMS" value="26" id="id_form-INITIAL_FORMS" /><input type="hidden" name="form-MIN_NUM_FORMS" value="0" id="id_form-MIN_NUM_FORMS" /><input type="hidden" name="form-MAX_NUM_FORMS" value="1000" id="id_form-MAX_NUM_FORMS" />
<input type="hidden" name="form-TOTAL_FORMS" value="26" id="id_form-TOTAL_FORMS" /><input type="hidden" name="form-INITIAL_FORMS" value="26" id="id_form-INITIAL_FORMS" /><input type="hidden" name="form-MIN_NUM_FORMS" value="0" id="id_form-MIN_NUM_FORMS" /><input type="hidden" name="form-MAX_NUM_FORMS" value="1000" id="id_form-MAX_NUM_FORMS" />
django 如何区分哪个管理表单用于哪个表单集?我怀疑这是导致我的验证错误的原因。有什么想法吗?
【问题讨论】:
-
抱歉删除我的答案。我看错了你的问题。
-
相反,您的回答很准确,解决了我的问题。我建议你重新发布你的答案,这样你就可以获得荣誉,并帮助其他遇到同样问题的用户。我的问题是认为前缀适用于表单集中的不同表单实例。当一个表单中有多个表单集时,我们的前缀是必要的。我原以为 django 开发人员会给每个管理表单一个唯一的名称。将是最合乎逻辑的解决方案。
-
猜我两次太仓促了!感谢您提醒我取消删除。
标签: django validation formset