Is it possible to aggregate the annotations of a queryset?
Models:
class Article(models.Model):
title = models.CharField(max_length=255)
body = models.TextField()
class State(models.Model):
article = Models.ForeignKey(Article)
date = DateField()
views = IntegerField()
downloads = IntegerField()
I'm trying to do the following:
articles = metrics_models.Article.objects.filter(
state__date__month=month,
state__date__year=year
).annotate(
views=Min('state__views'),
downloads=Min('state__downloads')
).aggregate(
views=Sum('views'),
downloads=Sum('downloads')
)
error:
Exception Type: DatabaseError
Exception Value:
column "downloads" does not exist
LINE 1: SELECT SUM(downloads), SUM(views) FROM (SELECT "metrics_arti...
When running this, I get a DatabaseError as django tries to do the aggregation on the 'views' and 'download' database columns instead of doing it on the annotations.
Is there any other way to do this aggregation on the QuerySet annotations?
...aggregate(total_views=Sum('views')...