简体   繁体   English

Django-在模板上显示模型中字段的过滤后总和

[英]Django - Display filtered sum of a field from a model on template

I have a model (camisaEvento) that have a One To Many relationship with (quantidadeEvento). 我有一个与(quantidadeEventEvento)有一对多关系的模型(camisaEvento)。 I want to display on the template the sum of a field in quantidadeEvento but only for the elements of a determined record on camisaEvento. 我想在模板上显示equiidadeEvento中一个字段的总和,但仅显示camisaEvento上已确定记录的元素。

On the view i have the following, which are passed to the template to a render_to_response: 在视图上,我有以下内容,这些内容传递到模板的render_to_response:

camisasEvento = camisaEvento.objects.all()
quantidadesEvento = quantidadeEvento.objects.all()

I'm making a table for each record of camisaEvento, and in the end i want it to have a cell that is the sum of a determined field on the quantidadeEvento but only for the current camisaEvento record on the for loop, so in the template i have the following: 我为camisaEvento的每条记录制作了一个表,最后我希望它具有一个单元格,该单元格是equiidadeEvento上确定字段的总和,但仅对于for循环上的当前camisaEvento记录,因此在模板中我有以下内容:

{% for camisa_do_evento in camisasEvento %}
<table>
    <td>...<td>
    <td>...<td>
    .
    .
    .
    <td>The sum should be in here</td>
<table>
{% endfor %}

What is the best way to do this? 做这个的最好方式是什么? I know this is probably simple, but i'm quite new to programming and Django. 我知道这可能很简单,但是我对编程和Django还是很陌生。

Thanks 谢谢

You should use the aggregation framework to calculate the sum, in the view: 您应该在视图中使用聚合框架来计算总和:

from django.db.models import Sum
camisasEvento = camisaEvento.objects.all().annotate(quantidade_sum=Sum('quantidadeevento__field_to_sum')

where 'field_to_sum' is the field you want to add up. 其中“ field_to_sum”是要添加的字段。 This creates an attribute called quantidade_sum on each camisasEvento that contains the sum of the related fields. 这将创建称为属性quantidade_sum每个camisasEvento包含相关领域的总和。

Note that in your template, you can iterate through the quantidadeEvento objects related to each camisaEvento by using the reverse relationship: 请注意,在模板中,您可以通过使用反向关系遍历与每个camisaEvento相关的QuantidadeEvento对象:

<table>
    {% for camisa_do_evento in camisasEvento %}
    <tr>
        {% for quantidade_evento in camisa_do_evento.quantidadeEvento.all %}
          <td>{{ quantidade_evento.my_field }}<td>
        {% endfor }
        <td>Sum: {{ camisa_do_evento.quantidade_sum }}</td>
    </tr>
    {% endfor %}
<table>

Why not using a property on camisaEvento? 为什么不在camisaEvento上使用属性?

class camisaEvento(models.Model):

    ...

    @property
    def sum_quantity(self):
        # just guessing, don't know exactly what you need
        # it's using python's list comprehension, but you can use a more explicit
        # syntax if you're not familiar with this one
        return sum([obj.field_to_sum for obj in               
                    quantidadeEvento.objects.filter(fk_field=self.id)])

and then, in your template: 然后,在您的模板中:

{% for camisa_do_evento in camisasEvento %}
    {{ camisa_do_evento.sum_quantity }}
{% endfor %}

How does this work: I'm creating a method on the camisaEvento class that fetches all the quantidadeEvento for that class and sums up their field (that's "fantasy" logic, since I don't exactly know what you need to sum). 工作原理:我在camisaEvento类上创建了一个方法,该方法将camisaEvento该类的所有quantidadeEvento并对其字段进行求和(这是“幻想”逻辑,因为我不完全知道您需要求和什么)。 Then, in your template, you just call that method as if it was a real property of your camisaEvento class 然后,在模板中,只需调用该方法,就好像它是camisaEvento类的真实属性一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM