我的视图从模板接收模型formset,但它没有通过验证,声称需要ID。到目前为止,我对形式的使用从未提出过这个问题,而且我从来没有通过身份证。
以下是我的观点的简化版本:
(Pdb) FormsetItem.errors
[{'id': ['This field is required.']}, {'id': ['This field is required.']}, {'id': ['This field is required.']}, {'id': ['This field is required.']}, {'name': ['This field is required.'], 'detail': ['This field is required.'], 'layout': ['This field is required.'], 'unit': ['This field is required.'], 'unit_description': ['This field is required.'], 'parent': ['This field is required.'], 'enName': ['This field is required.'], 'id': ['This field is required.']}, {'id': ['This field is required.']}, {'id': ['This field is required.']}, {'id': ['This field is required.']}, {'id': ['This field is required.']}, {'id': ['This field is required.']}, {'id': ['This field is required.']}, {'id': ['This field is required.']}, {'id': ['This field is required.']}, {'id': ['This field is required.']}, {'id': ['This field is required.']}, {'name': ['Ensure this value has at most 30 characters (it has 32).'], 'parent': ['Ensure this value has at most 30 characters (it has 32).'], 'id': ['This field is required.']}, {'id': ['This field is required.']}, {'id': ['This field is required.']}, {'id': ['This field is required.']}, {'id': ['This field is required.']}, {'id': ['This field is required.']}, {'id': ['This field is required.']}, {'id': ['This field is required.']}, {'id': ['This field is required.']}, {'id': ['This field is required.']}, {'id': ['This field is required.']}]
由于我上面提到的原因,is_valid返回FALSE。 任何想法?
根据要求,以下是从is_valid检查返回的错误:
{% block body %}
<div class="container" style="width:80%">
<form method="post">
{% csrf_token %}
{{ FormsetItem.management_form }}
{{ FormsetCat.management_form }}
<table>
<tr>
<th>פריט</th>
<th>מחיר מתוקצב</th>
<th>מיקום מחיר</th>
<th>רמת פירוט</th>
<th>רמת פירוט</th>
</tr>
<!--unpacks the item dictionary into formsets -->
{% for CatForm in FormsetCat %}
<tbody onmouseenter="ToggleDisable('{{ CatForm.enName.value}}')" onmouseleave="ToggleDisable('{{ CatForm.enName.value}}')">
{% for ItemForm in FormsetItem %}
{% if ItemForm.parent.value == CatForm.name.value %}
{% if ItemForm.layout.value == 'normal' %}
{% include 'getdata/normalBudgetLayout.html' with form=ItemForm itemCount=forloop.counter0 catagoryCount=forloop.parentloop.counter0 %}
{% elif ItemForm.layout.value == 'choice' %}
{% include 'getdata/choiceBudgetLayout.html' with form=ItemForm itemCount=forloop.counter0 catagoryCount=forloop.parentloop.counter0 %}
{% endif %}
{% endif %}
{% endfor %}
<tr class="txt_center Row_{{ CatForm.enName.value}}" style="line-height:4em; background:Silver;">
<td>
<a onclick="ToggleDisable('{{ CatForm.enName.value}}')">
{{ CatForm.name.value }}
<span style="float:left;">
<i class="fa fa-caret-down {{ CatForm.enName.value}}" style="font-size:30px; padding:7px;"></i>
<i class="fa fa-caret-up {{ CatForm.enName.value}}" style="font-size:30px; padding:7px;" hidden></i>
</span>
<!--/a-->
</td>
<td>{{ CatForm.catagory_cost }}</td>
<td>TBD</td>
<td>{{ CatForm.detail.value }}</td>
<td></td>
</tr>
</tbody>
{% endfor %}
</table>
<br />
<br />
<br />
<button type="submit" >Submit</button> <br />
<br />
</form>
</div>
{% endblock %}
这是我的模板:
{{1}}
THX
答案 0 :(得分:1)
ModelFormsets
需要form.id
。它呈现为隐藏字段。您将需要使用两个表单集来实现它。
{% for form in formset %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
<!-- form.visible fields go here -->
{% endfor %}
答案 1 :(得分:0)
在这种情况下,错误消息非常清楚:您必须呈现“id”字段,否则POST将不包含每条记录的主键值。
我建议使用crispy forms并让它渲染整个formset或手动渲染模板中的字段。 在第一种情况下,你会有这样的事情:
<form action="post" ...>
<table>
<tbody>
{% for form in formset %}
<tr>
<td>{{ form.field1 }}</td>
<td>{{ form.field2 }}</td>
<td class="hidden">{{ form.id }}</td>
</tr>
</tbody>
</table>
</form>
否则:
SelectQuery = self.Catalogo.select().where(self.Catalogo.c.SubcampoId == SubcampoId)
result = SelectQuery.execute()
for row in result:
value = row['Value']
答案 2 :(得分:0)
作为@unixo答案的附录,只需输入:
{{ form.id }}
没有任何周围的HTML标记,将在呈现模板时转换为以下内容(name
会生成value
,id
和formset_factory
的值):
<input type="hidden" name="form-1-id" value="2" id="id_form-1-id">
只需确保将其缩进for form in formset
循环中即可。
这意味着您无需添加class="hidden"
,除非您对所需的隐藏字段进行了某些特殊处理。