Django + PostGresSQL组按日期datetimefield [英] Django + PostGresSQL group by date on datetimefield
本文介绍了Django + PostGresSQL组按日期datetimefield的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如:
$ b我有一个模型,它有一个datetimefield,我试图按日期分组进行注释。 $ b
order_totals = Transfer.objects.filter(created__range = [datetime.datetime.combine(datetime.date.today(),datetime.time.min)+ datetime.timedelta( days = -5),datetime.datetime.combine(datetime.date.today(),datetime.time.max)])。values('created')。annotate(Count('id'))
上面的问题是每秒/毫秒的datetime字段分组,而不是日期。 >
任何想法我该怎么做?
干杯,
本
解决方案
您应该可以通过使用QuerySet.extra来解决此问题,并向查询添加列
例如
qs.filter(...)。extra(select = {'created_date':'created :: date '})。values('created_date')
I have a model which has a datetimefield that I'm trying to annotate on grouping by date.
Eg:
order_totals = Transfer.objects.filter(created__range=[datetime.datetime.combine(datetime.date.today(), datetime.time.min) + datetime.timedelta(days=-5), datetime.datetime.combine(datetime.date.today(), datetime.time.max)]).values('created').annotate(Count('id'))
The problem with the above is it groups by every second/millisecond of the datetime field rather then just the date.
Any ideas how would I do this?
Cheers, Ben
解决方案
You should be able to solve this by using QuerySet.extra and add a column to the query
eg.
qs.filter(...).extra(select={'created_date': 'created::date'}).values('created_date')
这篇关于Django + PostGresSQL组按日期datetimefield的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文