[我也在Django users | Google Groups上贴了这个.]
使用inline formset docs中的示例,我可以创建edit个属于特定模型的对象(使用
使用与上面链接中相同的示例,我将如何继续
[我也在Django users | Google Groups上贴了这个.]
使用inline formset docs中的示例,我可以创建edit个属于特定模型的对象(使用
使用与上面链接中相同的示例,我将如何继续
首先,创建一个作者模型表单.
author_form = AuthorModelForm()
然后创建虚拟作者对象:
author = Author()
然后使用虚拟作者创建一个内联表单集,如下所示:
formset = BookFormSet(instance=author) #since author is empty, this formset will just be empty forms
将其发送到模板.将数据返回视图后,创建作者:
author = AuthorModelForm(request.POST)
created_author = author.save() # in practice make sure it's valid first
现在将内联表单集与新创建的作者挂钩,然后保存:
formset = BookFormSet(request.POST, instance=created_author)
formset.save() #again, make sure it's valid first
编辑:
要在新表单上没有复选框,请执行以下操作:
{% for form in formset.forms %}
<table>
{% for field in form %}
<tr><th>{{field.label_tag}}</th><td>{{field}}{{field.errors}}</td></tr>
{% endfor %}
{% if form.pk %} {# empty forms do not have a pk #}
<tr><th>Delete?</th><td>{{field.DELETE}}</td></tr>
{% endif %}
</table>
{% endfor %}