[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 ...