如何在不使用额外费用的情况下按月计算总数?
我目前正在使用:
- django 1.8
- 博士后9.3.13
- Python 2.7
实例
What I have tried so far.
#Doesn't work for me but I don't mind because I don't want to use extra
truncate_month = connection.ops.date_trunc_sql('month','day')
invoices = Invoice.objects.filter(is_deleted = False,company = company).extra({'month': truncate_month}).values('month').annotate(Sum('total'))
----
#It works but I think that it's too slow if I query a big set of data
for current_month in range(1,13):
Invoice.objects.filter(date__month = current__month).annotate(total = Sum("total"))
还有这个,答案似乎很好,但我无法导入TruncMonth模块.
Django: Group by date (day, month, year)
另外,我知道这个问题已经被问了很多次,但我没有看到任何答案.
谢谢
SOLUTION:
多亏@Vin-G的回答.