0

how to make an ORM query with month and year of datefield.

select sum(price) from mytable group by month(start_date),year(start_date);

The above query which is giving the exact result. But in the ORM i tried but how to group by month and year wise.

My ORM query

 rv_per_month = Price_Report.objects.filter(item__in=item_Arr).aggregate(Sum('revenue'))

The above orm query which is giving the sum(price) . But I want it as group by month and year

4 fdvds
  • 97
  • 7

1 Answers1

-1
from collections import defaultdict
from decimal import Decimal
qs = Price_Report.objects.values_list("created_at", "amount")
tmp = defaultdict(Decimal)
for date, amount in qs:
   tmp[date.year, date.month] += amount
pod2metra
  • 256
  • 1
  • 6
  • I have tried it say's Cannot resolve keyword 'year' into field. Join on 'start_date' not permitted. – 4 fdvds Aug 24 '15 at 10:42
  • You can group you amount in python – pod2metra Aug 24 '15 at 10:48
  • Please consider editing your post to add more explanation about what your code does and why it will solve the problem. An answer that mostly just contains code (even if it's working) usually wont help the OP to understand their problem. – SuperBiasedMan Aug 24 '15 at 11:11