Django aggregation in templates?

后端 未结 2 1873
你的背包
你的背包 2021-01-26 01:05

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 mode

相关标签:
2条回答
  • 2021-01-26 01:25

    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

    0 讨论(0)
  • 2021-01-26 01:27

    The templates do not let you call functions that take arguments. See here for what they let you do.

    The canonical way to deal with this would be to either add some helper methods/managers to your models or to create a custom template tag or filter.

    0 讨论(0)
提交回复
热议问题