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 :)