问题描述
我想创建一个确认模型的表单。我已经成功地创建了formset,但是formset创建了一个额外的确认对象。
I want to create a formset of confirmation models. I've succesfully created the formset however formset creates an extra confirmation object.
这是我的代码:
VIEW
def render_fulfillment_modal(request,template='test.html'):
....
formset = modelformset_factory(Confirmation)
form = formset(queryset=Confirmation.objects.filter(customer_order__deal = deal))
if request.method == 'POST':
form = formset(request.POST, request.FILES)
if form.is_valid():
form.save()
TEMPLATE
<form method="post" action="{% url open_fullfill_modal deal.id %}">{% csrf_token %}
{{ form.management_form }}
{% for f in form %}
<tr>
<td>{{f.fullfilled}}</td>
<td>
<p class="name">{{f.instance|confirmation_user_info}}</p>
</td>
<td><input type="text" class="input-small datepicker"></td>
<td>{{f.tracking_code}}</td>
</tr>
{% endfor %}
<div class="pull-right button-box">
<button type="submit" class="btn btn-primary btn-large">Save Changes</button>
</div>
对于不在我的查询中的不相关对象的表单。我已经尝试了另一个模型,每次我得到一个额外的对象。我想这是与formets处理数据或某事的东西,我不知道。当我发布此表单时,会出现此问题。它给了我MultiValueDictKeyError,它是:
I'm getting an extra form for unrelated object which is not in my queryset. I've tried this with another models and each time I'm getting an extra object. I suppose it's something with the formsets to handle data or something, I'm not sure. The problem occurs when I post this form. It gives me MultiValueDictKeyError which is :
"Key 'form-0-id' not found in <QueryDict: {u'form-MAX_NUM_FORMS': [u''], u'form-TOTAL_FORMS': [u'3'] ...
任何想法?
推荐答案
只要放一个 {{f.id}}
before {{f.fullfilled}}
Just put a {{f.id}}
before {{f.fullfilled}}
它被赋予一个隐藏的表单对于所有f,并将其传递给QueryDict请求.Post
It's given a hidden form-id for all f, and pass it to QueryDict in request.Post
这篇关于Django模型formset查询生成额外的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!