我的模板的樣子:格式表單集的結果值不輸入
<form method="post" action="">
{{ formset.management_form }}
{% for form in formset.forms %}
{{ form.contractor }} {{ form.date }} {{ form.value }} {{ form.comment }} {{ form.operation_type }} {{ form.category }} {{ form.account }}
{% endfor %}
</form>
,但結果卻讓改變所有領域的 - 但我想只有一個。
我認爲(請注意「.value」後的所有類別字段)解決問題,但不是。
<form method="post" action="">
{{ formset.management_form }}
{% for form in formset.forms %}
{{ form.contractor.value }} {{ form.date.value }} {{ form.value.value }} {{ form.comment.value }} {{ form.operation_type.value }} {{ form.category }} {{ form.account.value }}
{% endfor %}
</form>
UPD: 相關視圖代碼
def detail(request, category_id):
from django.forms.models import modelformset_factory
OperationFormSet = modelformset_factory(Operation)
if request.method == "POST":
formset = OperationFormSet(request.POST, request.FILES,
queryset=Operation.objects.filter(category=category_id))
if formset.is_valid():
formset.save()
# Do something.
else:
formset = OperationFormSet(queryset=Operation.objects.filter(category=category_id))
return render_to_response("reports/operation_list.html", {
"formset": formset,
})
請發表相關的檢視代碼。 – patrickn