模板中的Django聚合? [英] Django aggregation in templates?

查看:20
本文介绍了模板中的Django聚合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在思考 Django 聚合的概念. 我不太明白"如何在我的情况下使用它们.基本上,我的模型中有一个三层对象层次结构,最低的对象 (Bar) 包含我想要聚合的值.

I'm thinking a bit about the concept of Django aggregates. I don't quite "get" how they can be used in my case. Basically i have a three-tier hierarchy of objects in my model, and the lowest object (Bar) contain values I want to aggregate.

class Bar(models.Model):
    amount = models.FloatField()

class Foo(models.Model):
    bars = models.ManyToManyField(Bar)

class MyUser(models.Model):
    foos = models.ManyToManyField(Foo)

我希望我的模板能做到这一点:

I want my template to do the equivalent of this:

{% for user in users %}
    <h2>{{ user }}</h2>
    <table>
        <tr>
            <td>Foo</td><
            <td>Average bar amount</td>
        </tr>
    {% for foo in user.foos.all %}
        <tr>
            <td>{{ foo }}</td>
            <td>{{ foo.bars.all.aggregate(Avg('amount')) }}
        </tr>
    {% endfor %}
    </table>
{% endfor %}

当然,这个模板只是伪代码,它不会工作,因为我猜模板不应该按照设计来做到这一点.但另一方面,在我看来,必须预先计算平均金额也会让人感觉不对.应该如何解决这个问题?

But of course this template is just pseudo code, it won't work because, I guess, templates are not supposed to do this by design. But on the other hand, having to pre-calculate the average amounts in my view would feel wrong too. How should this problem be approached?

我还是 Django 的新手,所以我想知道这样做的Django 方式":)

I'm still new to Django so I want to know the "Django way" of doing this :)

推荐答案

这项工作应该在视图中完成.你说这感觉不对,但这正是视图的目的:检索将要呈现给用户的数据.看看Django FAQ:http://docs.djangoproject.com/en/dev/faq/general/#django-appears-to-be-a-mvc-framework-but-you-call-the-controller-the-view-and-the-view-the-template-how-come-you-不要使用标准名称

This work should be done in the view. You say that feels wrong but that's exactly what the view is meant to do: retrieve the data that is going to be presented to the user. Take a look at the Django FAQ: http://docs.djangoproject.com/en/dev/faq/general/#django-appears-to-be-a-mvc-framework-but-you-call-the-controller-the-view-and-the-view-the-template-how-come-you-don-t-use-the-standard-names

Django 看起来是一个 MVC 框架,但是你调用 Controller视图"和视图"模板".你怎么不用标准名称?

在我们对 MVC 的解释中,view"描述了获取的数据呈现给用户.不是必然是数据的样子,但是呈现哪些数据.风景描述您看到哪些数据,而不是如何你看到了.这是一个微妙的区别.

In our interpretation of MVC, the "view" describes the data that gets presented to the user. It’s not necessarily how the data looks, but which data is presented. The view describes which data you see, not how you see it. It’s a subtle distinction.

另一方面,根据您在模板中使用平均值的方式,使用 annotate 而不是 aggregate 可能更有意义.http://docs.djangoproject.com/en/1.1/ref/models/querysets/#annotate-args-kwargs

A side point to this is that based on how you are using the average in your template it would probably make more sense to use annotate rather than aggregate. http://docs.djangoproject.com/en/1.1/ref/models/querysets/#annotate-args-kwargs

这篇关于模板中的Django聚合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆