<p>正如Iulia在另一个答案中所说,<code>MaterialRequest</code>和{<cd2>}之间的关系是外键,意味着一个{<cd1>}可以有多个{<cd2>}。如果发生这种情况,那么可以使用<a href="https://docs.djangoproject.com/en/2.2/topics/db/aggregation/#following-relationships-backwards" rel="nofollow noreferrer"><strong>^{<cd5>}</strong></a>在单个查询中使用<a href="https://docs.djangoproject.com/en/2.2/topics/db/aggregation/" rel="nofollow noreferrer"><strong>^{<cd10>}</strong></a>从反向关系中求出{<cd6>}、<code>side_wall,</code><code>top_lid</code>、<code>insert</code>的值。例如:</p>
<pre><code><b>from django.db.models import Sum</b>
client_pk = Client.objects.filter(client_name = value)[0]
mat_req = MaterialRequest.objects.filter(
owner=client_pk.pk,
created_on__range=[start, end]
)<b>.annotate(
plastic_pallet=Sum('allotment_parent__plastic_pallet')
).annotate(
side_wall=Sum('allotment_parent__side_wall')
).annotate(
top_lid=Sum('allotment_parent__top_lid')
).annotate(
insert=Sum('allotment_parent__insert')
)</b></code></pre>
<p>并在模板中使用:</p>
^{pr2}$
<p><em>顺便说一句(或者可能不相关)</em>,如果<code>MaterialRequest</code>和{<cd2>}之间存在{a3}关系,那么您只需在模板中使用<code>{{ i.allotment_parent.side_wall }}</code>来获取{<cd15>}信息。在</p>