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

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

问题描述

我正在考虑一下 Django聚合的概念。我不太会得到他们如何在我的情况下使用。基本上我的模型中有一个三层层次的对象,最低的对象(Bar)包含我要聚合的值。

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

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

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

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

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

但当然这个模板只是伪代码,它赢了因为,我想,模板不应该这样设计。但另一方面,在我看来,不得不预先计算平均数额也会觉得错误。这个问题应该如何处理?



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

解决方案

这个工作应该在视图中完成。你说感觉很错,但这正是视图的意图:检索要呈现给用户的数据。看看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-name


Django似乎是一个MVC框架,但是您调用Controller
view,并查看
template。您如何不使用
标准名称?



在我们对MVC的解释中,
视图描述了向用户提供
的数据。数据看起来不是
,而是
显示哪些数据。视图
描述了您看到的数据,而不是您看到的
。这是一个微妙的区别。


这一点是根据您在模板中使用平均值的方式,它可能会更有意义的是使用注释而不是聚合 http://docs.djangoproject.com/en /1.1/ref/models/querysets/#annotate-args-kwargs


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?

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

解决方案

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 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?

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.

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天全站免登陆