Need some help on fixing the following error. I recently moved to 1.8alpha version from an older version django-e9d1f11 and I started seeing this error. Looks like the way group by is constructed has changed.
Here is the model that I have.class everydaycost(models.Model):
cost = models.DecimalField(max_
rev = models.DecimalField(max_
ts = models.DateField(db_index=
I am trying to get roi (rev/cost) for each day of week.
$ python manage.py shell
Python 2.7.5 (default, Mar 9 2014, 22:15:05)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>>
>>> from testgroupby.models import *
>>> from django.db.models import Q, Count, Min, Sum, Func, FloatField
>>> qs = everydaycost.objects.all()
>>> qs = qs.extra(select={'dow': "TO_CHAR(ts, 'D')"})
>>> qs = qs.values('dow')
>>> qs = qs.annotate(sum_cost=Sum('
>>> qs = qs.annotate(sum_rev=Sum('rev')
>>> qs = qs.annotate(roi=Func(A='CAST(
... template='ROUND(COALESCE(%(A)s / NULLIF(%(B)s,0), 0), 2)', output_field=FloatField()))
>>> print qs
DEBUG (0.002) SELECT (TO_CHAR(ts, 'D')) AS "dow", SUM("testgroupby_everydaycost"
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Downloads/Django-1.8a1/
data = list(self[:REPR_OUTPUT_SIZE + 1])
File "/Downloads/Django-1.8a1/
self._fetch_all()
File "/Downloads/Django-1.8a1/
self._result_cache = list(self.iterator())
File "/Downloads/Django-1.8a1/
for row in self.query.get_compiler(self.
File "/Downloads/Django-1.8a1/
results = self.execute_sql(MULTI)
File "/Downloads/Django-1.8a1/
cursor.execute(sql, params)
File "/Downloads/Django-1.8a1/
return super(CursorDebugWrapper, self).execute(sql, params)
File "/Downloads/Django-1.8a1/
return self.cursor.execute(sql, params)
File "/Downloads/Django-1.8a1/
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/Downloads/Django-1.8a1/
return self.cursor.execute(sql, params)
ProgrammingError: aggregate functions are not allowed in GROUP BY
LINE 1: ... GROUP BY (TO_CHAR(ts, 'D')), ROUND(COALESCE(CAST(Sum("rev")
>>> print qs.query
SELECT (TO_CHAR(ts, 'D')) AS "dow", SUM("testgroupby_everydaycost"
>>>
$ python manage.py shell
Python 2.7.5 (default, Mar 9 2014, 22:15:05)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>>
>>> from testgroupby.models import *
>>> from django.db.models import Q, Count, Min, Sum, Func, FloatField
>>> qs = everydaycost.objects.all()
>>> qs = qs.extra(select={'dow': "TO_CHAR(ts, 'D')"})
>>> qs = qs.values('dow')
>>> qs = qs.annotate(sum_cost=Sum('
>>> qs = qs.annotate(sum_rev=Sum('rev')
>>> qs = qs.annotate(roi=Func(A='CAST(
... template='ROUND(COALESCE(%(A)s / NULLIF(%(B)s,0), 0), 2)', output_field=FloatField()))
>>> print qs
DEBUG (0.002) SELECT (TO_CHAR(ts, 'D')) AS "dow", SUM("testgroupby_everydaycost"
[]
>>> print qs.query
SELECT (TO_CHAR(ts, 'D')) AS "dow", SUM("testgroupby_everydaycost"
>>>
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a69957e1-8238-4aaa-bc9e-61e512335d16%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment