if have model book defined as:
class book(models.model): name = models.charfield(max_length=300) pages = models.integerfield() price = models.decimalfield(max_digits=10, decimal_places=2) rating = models.floatfield() pubdate = models.datefield() and run query:
book.objects.values('rating').annotate(books_per_rating=count('id')).aggregate(max('books_per_rating')) i databaseerror.
according https://docs.djangoproject.com/en/1.5/topics/db/aggregation/#aggregating-annotations, django supports aggregating annotations. in example given in link itself, annotate on queryset in turn returns queryset (and not valuesqueryset), aggregate method runs successfully. in example aggregating valuesqueryset raises databaseerror.
is bug in django? because if django not support aggregation on valuesqueryset should raise exception @ django level (not databaseerror).
update:
this bug has been solved here: https://code.djangoproject.com/ticket/20782
it appears you've ran bug. should log on django trac instance.
be specific db backend you're using. i'm testing against sqlite @ moment, since don't have access different backend. mention because i've seen usage of values().aggregate() working while searching answer question - may bug in sqlite sql compiler django uses.
here test using, , output generated query.
>>> stats.objects.values('created').annotate(num_days=count('id')).aggregate(max('num_days')) traceback (most recent call last): databaseerror: near "from": syntax error >>> print connection.queries[-1] {u'time': u'0.000', u'sql': u'select (select "scratch_stats"."created" "created", count("scratch_stats"."id") "num_days" "scratch_stats" group "scratch_stats"."created") subquery'} edit:
just tried same thing postgres, , same error. appears can't use aggregate valuesqueryset.
fails on django 1.4, 1.5 , 1.6.
Comments
Post a Comment