We're currently using Django 1.8 and Postgres.
I have an aggregation that I want to perform, but I want the aggregations grouped by month. This is trivial to do in SQL, but I can't seem to figure out any way to go about it with the Django ORM.
Here's an example of the SQL query I'm performing that provides the desired results(SQL Fiddle):
SELECT
EXTRACT(month from date) as month,
EXTRACT(year from date) as year,
SUM(total) as total
FROM transaction
GROUP BY year, month ORDER BY year, month;
And here's an example of my translation to Django(this one is using the Month
class from this answer, but I've tried several variations):
results = Transactions.all().annotate(month=Month('date')).aggregate(total=Sum('total', output_field=DecimalField()))
There's some additional aggregating going on here, but I removed it for clarity. I don't care what the Django output would end up looking like as long as it's grouped by month.