I have two querysets:
category_list_avg =Category.objects.filter(operation__date__gte = year_ago).annotate(podsuma = Sum('operation__value'))
category_list = Category.objects.annotate(suma = Sum('operation__value'))
Is there a way to do this with one queryset (there is a for loop in my template)?
UPD:
template:
{% for category in category_list %}
<tr>
<td><a href="{{ category.id }}/">{{ category.name }}</a></td>
<td style="text-align:right{% if category.suma|default:0 < 0 %};color:red{% endif %}">
{{ category.suma|default:0|currency }}</td>
{% for monthsum in category.get_month_sum_series %}
<td style="text-align:right{% if monthsum.1 < 0 %};color:red{% endif %}">{{ monthsum.1|currency }}</td>
{% endfor %}
<td style="text-align:right">???</td>
</tr>
{% endfor %}
in place of ??? would be the average value (now I use sum of subpart - because its easier to debug - if I see that podsuma is less than suma - I would know that it works.
You need to do another query to get average values and attach them to
category_list
, either in view or in model:Then use
{{ category.one_year_ago_avg }}
in template