django model group by day

query = C1.objects.filter(createTime__range=(start_date, end_date)).extra(select={'year': "EXTRACT(year FROM createtime)",
                                              'month': "EXTRACT(month from createtime)",
                                              'day': "EXTRACT(day from createtime)"}

                                      ).values('year', 'month', 'day').annotate(Count('id'))

SQL:

select count(id),
extract(year from createtime) as year,
extract(month from createtime) as month,
extract(day from createtime) as day
from table 
group by year, month, day
;

结果

[{'year': 2012L, 'id__count': 14, 'day': 17L, 'month': 5L}, {'year': 2012L, 'id__count': 4, 'day': 18L, 'month': 5L}, {'year': 2012L, 'id__count': 4, 'day': 22L, 'month': 5L}]

你可能感兴趣的:(sql,c,Date,django,query)