首页 > 解决方案 > 如何聚合如果

问题描述

在 django 模板中,我可以访问价格,{% for x in done %}然后访问产品,这是产品表的外键。所以我像这样访问产品的价格{{x.productt.omzet}}

我的问题是:我该如何总结它们?

在views.py 中,我成功地使用了聚合函数来对子表中的值求和;但是,在这种情况下,我需要从一个大子表中聚合值。我未能在views.py 的for 循环中做到这一点。

def factuur(request, id):
factuur = Factuur.objects.get(id=id)
leerling = Leerling.objects.get(name=factuur.leerling)
jezelf = Leerling.objects.get(id=2)
paid = BankTransactions.objects.filter(actor=factuur.leerling)
total_paid = paid.aggregate(Sum('amount'))
done = Product.objects.filter(leerling=factuur.leerling)
total_done = done.aggregate(Sum('productt'))

if leerling.status == 4:
    bedrag1 = "done - paid = factuur"
elif leerling.status == 3:
    bedrag1 = "factuur is any"
else:
    bedrag1 = "factuur is niet van toepassing"
notice = bedrag1

context = {'factuur': factuur,
           'leerling': leerling,
           'jezelf': jezelf,
           'paid': paid,
           'done': done,
           'total_paid': total_paid,
           'total_done': total_done,
           'notice': notice,
           }
return render(request, 'rozendale/factuur.html', context)

模板:

<div class="row">
        <div class="col-lg-6">
            <table>
                <tr>
                    <th>Datum</th>
                    <th>Service</th>
                    <th>Bedrag</th>
                </tr>
                {% for x in done %}
                <tr>
                    <td>
                        <a href="/item/{{x.id}}">{{x.date|date:"SHORT_DATE_FORMAT"}}</a>
                    </td>
                    <td>
                        <a href="/product/{{x.productt.id}}">{{x.productt.name}}</a>
                    </td>
                    <td>
                        {{x.productt.omzet}}
                    </td>
                </tr>
               {% endfor %}
                <tr>
                    <th colspan="2">Totaal:</th>
                    <td>
                    {{total_done}}
                    </td>
                </tr>
            </table>
        </div>
        <div class="col-lg-6">
            <table>
                <tr>
                    <th>Datum</th>
                    <th>Factuur</th>
                    <th>Betaald</th>
                </tr>
                {% for x in paid %}
                <tr>
                    <td>
                        <a href="/item/{{x.id}}">{{x.date|date:"SHORT_DATE_FORMAT"}}</a>
                    </td>
                    <td>
                        <a href="/factuur/{{x.factuur.id}}">{{x.factuur}}</a>
                    </td>
                    <td>
                        {{x.amount}}
                    </td>
                </tr>
               {% endfor %}
                <tr>
                    <th colspan="2">Totaal:</th>
                    <td>{{total_paid.amount__sum}}</td>
                </tr>
            </table>
        </div>

标签: djangotemplatessumaggregate

解决方案


视图.py

def factuur(request, id):
factuur = Factuur.objects.get(id=id)
leerling = Leerling.objects.get(name=factuur.leerling)
yourself= Leerling.objects.get(id=2)

paid = BankTransactions.objects.filter(actor=factuur.leerling)
total_paid = []
for pay in paid:
    total_paid += [(pay.amount)]
total_paid = sum(total_paid)

done = Product.objects.filter(leerling=factuur.leerling)
total_done = []
for les in done:
    dit = ProductName.objects.get(id=les.productt_id)
    total_done += [(dit.omzet)]
total_done = sum(total_done)

openstaand = total_done - total_paid
#notice = factuur.amount * factuur.productt.omzet

if leerling.status == 4:
    notice = openstaand
elif leerling.status == 3:
    notice = factuur.amount * factuur.productt.omzet
else:
    notice = "factuur is niet van toepassing"

context = {'factuur': factuur,
           'leerling': leerling,
           'yourself': yourself,
           'paid': paid,
           'done': done,
           'openstaand': openstaand,
           'total_paid': total_paid,
           'total_done': total_done,
           'notice': notice,
           }
return render(request, 'test/factuur.html', context)

事实 = 发票。这是发票的详细视图 ( object..get(id=id))。该发票属于一个客户,但属于许多产品和许多交易。在这段代码中,我过滤了属于该客户的所有内容。我用 for 循环做到了。然后在 for 循环中,我遍历客户的过滤产品和交易。在每次迭代中,我使用 GET 方法来获取产品的价格。但该价格在另一个表中:产品表(详细信息)。所以实际上,该产品的价格来自孙表。


推荐阅读