[django]按月分组统计数据

假设有以下 model:

class Foobar(models.Model):
    name = models.CharField(max_length=100)
    date_created = models.DateField(auto_now_add=True)

    def __unicode__(self):
        return self.name

按月统计数量的代码如下:

from django.db import connection
from django.db.models import Count

select = {'month': connection.ops.date_trunc_sql('month', 'date_created')}
Foobar.objects.extra(select=select).values('month').annotate(number=Count('id'))
# [{'number': 10, 'month': datetime.datetime(2013, 9, 1, 0, 0)}]

参考

有任何意见欢迎在下方留言或者加我微信交流


Comments

comments powered by Disqus