Hrm... didn't quite work...
In [18]: Alpha.objects.filter(gamma__beta__name='Beta').values('name')
Out[18]: [{'name': u'Alpha'}, {'name': u'Alpha'}]
In [19]:
Alpha.objects.filter(gamma__beta__name='Beta').values('name').annotate(subtotal=models.Sum('id'))
Out[19]: [{'subtotal': 2, 'name': u'Alpha'}]
In [24]:
Alpha.objects.filter(gamma__beta__name='Beta').values('name').annotate(subtotal=models.Sum('id')).aggregate(total=models.Sum('subtotal'))
---------------------------------------------------------------------------
DatabaseError Traceback (most recent call
last)
/#snip#/<ipython console> in <module>()
/usr/local/lib/python2.6/dist-packages/django/db/models/query.pyc in
aggregate(self, *args, **kwargs)
311 is_summary=True)
312
--> 313 return query.get_aggregation(using=self.db)
314
315 def count(self):
/usr/local/lib/python2.6/dist-packages/django/db/models/sql/query.pyc
in get_aggregation(self, using)
364 query.related_select_fields = []
365
--> 366 result = query.get_compiler(using).execute_sql(SINGLE)
367 if result is None:
368 result = [None for q in
query.aggregate_select.items()]
/usr/local/lib/python2.6/dist-packages/django/db/models/sql/
compiler.pyc in execute_sql(self, result_type)
725
726 cursor = self.connection.cursor()
--> 727 cursor.execute(sql, params)
728
729 if not result_type:
/usr/local/lib/python2.6/dist-packages/django/db/backends/util.pyc in
execute(self, sql, params)
17 start = time()
18 try:
---> 19 return self.cursor.execute(sql, params)
20 finally:
21 stop = time()
/usr/local/lib/python2.6/dist-packages/django/db/backends/sqlite3/
base.pyc in execute(self, query, params)
198 query = self.convert_query(query)
199 try:
--> 200 return Database.Cursor.execute(self, query,
params)
201 except Database.IntegrityError, e:
202 raise utils.IntegrityError,
utils.IntegrityError(*tuple(e)), sys.exc_info()[2]
DatabaseError: near "FROM": syntax error
Looks like it really doesn't like that aggregate. I might be confused,
but it seems like it wouldn't work anyway, since the annotation
produces 'subtotal':2, where what I'm going for would be 'subtotal':1,
since there's only one distinct model, but I might be missing
something.
It seems like any call to aggregate after values and annotate produces
this error (same error, same line), regardless of whether it
references the annotation.
In [25]:
Alpha.objects.filter(gamma__beta__name='Beta').values('id').annotate(subtotal=models.Sum('id')).aggregate(total=models.Sum('subtotal'))
---------------------------------------------------------------------------
DatabaseError Traceback (most recent call
last)
In [26]:
Alpha.objects.filter(gamma__beta__name='Beta').values('id').annotate(subtotal=models.Sum('id')).aggregate(total=models.Sum('id')
....: )
---------------------------------------------------------------------------
DatabaseError Traceback (most recent call
last)
And I dunno if this is related, but probably is... aggregate fails to
produce anything when called after values:
In [28]: Alpha.objects.filter(gamma__beta__name='Beta').values('id')
Out[28]: [{'id': 1}, {'id': 1}]
In [29]:
Alpha.objects.filter(gamma__beta__name='Beta').values('id').aggregate(models.Sum('id'))
Out[29]: {}
In [30]:
Alpha.objects.filter(gamma__beta__name='Beta').values('name').aggregate(models.Sum('id'))
Out[30]: {}
In [31]:
Alpha.objects.filter(gamma__beta__name='Beta').aggregate(models.Sum('id'))
Out[31]: {'id__sum': 2}
Any other ideas? It's seeming more and more like if I want this done
in the DB, I'm going to have to write some custom SQL. Either that or
do post processing in python after getting the distinct queryset.
- Jake
P.S. Thanks for your attempted help. :)