我想创建嵌套表单,最好通过以下提供的示例进行描述:
http://yergler.net/blog/2009/09/27/nested-formsets-with-django/
此页面的教程似乎相当不错&&它试图完成我遇到的确切问题。
当没有POST请求数据时(例如,我们正在从数据库执行初始填充),views.py文件中的此实现似乎存在问题。
可以在上面提供的URL上看到代码(如果需要,我可以发布一些代码,但我担心它会从这里提供的信息中删除)。
以下是失败的views.py代码(粗体):
block = get_object_or_404(models.Block, id=block_id)
if request.method == 'POST':
formset = forms.BuildingFormset(request.POST, instance=block)
if formset.is_valid():
rooms = formset.save_all()
return redirect('block_view', block_id=block.id)
else:
formset = forms.BuildingFormset(instance=block) #This is the line that is throwing the ValidationError
我得到的错误信息是:
ValidationError at "urlName":
[u'ManagementForm data is missing or has been tampered with']
我已经挖得更深了,看来这个失败发生在line site-packages / django / forms / formsets.py
is_valid()
检查失败,因为某些管理层需要的数据(form-TOTAL_FORMS,form-INITIAL_FORMS和form-MAX_NUM_FORMS)无效。以下是self.errors的实际输出:
{u'TOTAL_FORMS': [u'This field is required.'], u'INITIAL_FORMS': [u'This field is required.']}
edit_building.html:
{{buildings.management_form}}
{building for buildings.forms%}
{{ building }} {% if building.nested %} {% for formset in building.nested %} {{ formset.as_table }} {% endfor %} {% endif %}
{%endfor%}
views.py:
def should_delete(self, form):
"""Convenience method for determining if the form’s object will
be deleted; cribbed from BaseModelFormSet.save_existing_objects."""
if self.can_delete:
raw_delete_value = form._raw_value(DELETION_FIELD_NAME)
should_delete = form.fields[DELETION_FIELD_NAME].clean(raw_delete_value)
return should_delete
return False
def save_all(self, commit=True):
"""Save all formsets and along with their nested formsets."""
# Save without committing (so self.saved_forms is populated)
# — We need self.saved_forms so we can go back and access
# the nested formsets
objects = self.save(commit=False)
# Save each instance if commit=True
if commit:
for o in objects:
o.save()
# save many to many fields if needed
if not commit:
self.save_m2m()
# save the nested formsets
for form in set(self.initial_forms + self.saved_forms):
if self.should_delete(form): continue
for nested in form.nested:
nested.save(commit=commit)
forms.py:
def should_delete(self, form):
"""Convenience method for determining if the form’s object will
be deleted; cribbed from BaseModelFormSet.save_existing_objects."""
if self.can_delete:
raw_delete_value = form._raw_value(DELETION_FIELD_NAME)
should_delete = form.fields[DELETION_FIELD_NAME].clean(raw_delete_value)
return should_delete
return False
def save_all(self, commit=True):
"""Save all formsets and along with their nested formsets."""
# Save without committing (so self.saved_forms is populated)
# — We need self.saved_forms so we can go back and access
# the nested formsets
objects = self.save(commit=False)
# Save each instance if commit=True
if commit:
for o in objects:
o.save()
# save many to many fields if needed
if not commit:
self.save_m2m()
# save the nested formsets
for form in set(self.initial_forms + self.saved_forms):
if self.should_delete(form): continue
for nested in form.nested:
nested.save(commit=commit)
我已经查看了https://docs.djangoproject.com/en/dev/topics/forms/formsets/#understanding-the-managementform的django文档,但没有找到任何有用的内容来讨论DJANGO如何自动填充这些值
我正在使用Django V1.5
如果没有POST数据且表单仅由数据库生成,那么'form-TOTAL_FORMS'&&正确填写'form-INITIAL_FORMS'数据来解决这个失败?
答案 0 :(得分:4)
<强>更新强>
在查看了the example之后,我在forms.py
方法的末尾add_fields()
中显示了一个如下所示的代码段:
# store the formset in the .nested property
form.nested = [
TenantFormset(data = self.data,
instance = instance,
prefix = 'TENANTS_%s' % pk_value)
]
data
参数导致问题,因为它最初是空的,并且Django内部将通过与此类似的条件确定表单is bound:
self.is_bound = data is not None
# Example
>>> my_data = {}
>>> my_data is not None
True
正如您所看到的,Python中的空字典不是None,因此即使不是TenantFormset
,bound
也会被视为# store the formset in the .nested property
form.nested = [
TenantFormset(data = self.data if any(self.data) else None,
instance = instance,
prefix = 'TENANTS_%s' % pk_value)
]
形式。您可以使用以下内容修复它:
<form method="post">
{{ formset.management_form }}
<table>
{% for form in formset %}
{{ form }}
{% endfor %}
</table>
</form>
您可以发布视图和表单代码以及表单的模板代码吗?
我的猜测是你没有在模板中使用'management_form'(它添加了你缺少的“form-TOTAL_FORMS”和“form-INITIAL_FORMS”字段),即
{{1}}