Here's the code I've put together for the ORM so far:
{{{
span="month"
truncate_date = django.db.connection.ops.date_trunc_sql(span, "date")
print Transaction.objects.extra({span: truncate_date}).values("category",
span).annotate(total=Sum("amount")).query
}}}
This results in:
{{{
SELECT (django_date_trunc('month', date)) AS "month",
"finance_transaction"."category_id", SUM("finance_transaction"."amount")
AS "total" FROM "finance_transaction" GROUP BY
"finance_transaction"."category_id", "finance_transaction"."date",
(django_date_trunc('month', date)) ORDER BY "finance_transaction"."date"
DESC
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/21182>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0
Comment:
I'm skeptical of your use of `date_trunc_sql` -- this isn't a public API
that's designed for general use as far as I know.
Assuming there is a bug in Django though, it would be helpful if you could
include a test for Django's test suite so we can more easily reproduce the
issue.
--
Ticket URL: <https://code.djangoproject.com/ticket/21182#comment:1>
* status: new => closed
* resolution: => needsinfo
Comment:
Closing as needs info in absence of a test to reproduce. Feel free to
reopen if you can provide more details, thanks!
--
Ticket URL: <https://code.djangoproject.com/ticket/21182#comment:2>
Comment (by shai):
For anyone who looks at this later:
The query produced includes `ORDER BY "finance_transaction"."date" DESC`,
which hints that the user's Transaction model has a meta
`ordering=["-date"]`; if this is the case, the date field is
'''correctly''' added to the group-by, to allow this ordering. Ordering by
month instead should solve the problem.
--
Ticket URL: <https://code.djangoproject.com/ticket/21182#comment:3>