Django:Formset保存的数据单个原始检索

时间:2016-04-22 16:26:37

标签: django django-templates django-views

我正在为应用程序使用Inlineformsets。数据被保存,但是当我检索数据时,正确检索主表单数据,但是在子模型的情况下,只从DB中检索第一个原始数据。

Views.py

StringIO

template.html

@login_required
def invoice_detail(request,pk):  


    invoice_detailmain = serviceinvoice.objects.get(pk=pk)
    if invoice_detailmain.user != request.user:
        raise PermissionDenied
    invoiceitems =invoice_detailmain.serviceitems1.filter(pk=pk,user=request.user)

    return render(request,'account/invoicedetail.html',{'invoice_detailmain':invoice_detailmain,
                                                        'invoiceitems':invoiceitems})

models.py

{% for items in invoiceitems %}
<tr><td>{{ items.Product }}</td>
<td>{{ items.UOM }}</td>
<td>{{ items.Quantity }}</td>
<td>{{ items.Rate }}</td>
<td>{{ items.Tax_rate }}</td></tr>
{% endfor %}

1 个答案:

答案 0 :(得分:1)

我认为您的过滤不正确。而不是

invoiceitems = invoice_detailmain.serviceitems1.filter(pk=pk,user=request.user)

你应该使用

invoiceitems = invoice_detailmain.serviceitems1.filter(user=request.user)

虽然我感到困惑,为什么你有serviceinvoiceserviceinvoiceitems的主键给你的用户模型。您可能已经足够指定

invoiceitems = invoice_detailmain.serviceitems1.all()