我正在努力获得按日分组的注册用户数量,但到目前为止失败了。
xlApp = Dispatch("Excel.Application")
wb1=xlApp.Workbooks.Open(inputfile,ReadOnly=1,CorruptLoad=1)
xlApp.SendKeys("{Enter}",Wait=1)
xlApp.DisplayAlerts = 0
xlApp.Quit()
del xlApp
以下原始sql工作正常:
class Profile(models.Model):
createdon=models.DateField()
现在的输出是:
SELECT COUNT(day(createdon)),day(createdon) from members_profile where month(createdon)=11 and year(createdon)=2017 GROUP BY day(createdon)
在ORM中,我最接近的是:
1 10
5 8
但这并不令人惊讶,因为id是唯一的字段。我反而希望按照' d'分组,注释列投诉无法解析。
答案 0 :(得分:2)
尝试使用以下
from django.db.models.functions import TruncDay
from django.db.models import Count
(Profile.objects.annotate(day=TruncDay('createdon'))
.values('day')
.annotate(count=Count('id'))
.order_by())