python 按月份分组_django ORM queryset按月、周、TruncMonth分组
如何在Django ORM中對datetime字段進(jìn)行g(shù)roup by查詢?在
型號:class test1(models.Model):
id = models.AutoField(primary_key=True, unique=True, verbose_name='id')
name = models.CharField(verbose_name='name', max_length=200)
cdate = models.DateField(verbose_name='date', default=timezone.now)
查詢集數(shù)據(jù)
^{pr2}$
需要按月分組并用Django orm計(jì)算id。在
奎里塞特data = test1.objects.annotate(month=TruncMonth('cdate')).values('month').annotate(c=Count('id'))
for i in data:
print i
結(jié)果{'c': 1, 'month': datetime.date(2017, 8, 1)}
{'c': 1, 'month': datetime.date(2017, 8, 1)}
{'c': 1, 'month': datetime.date(2017, 8, 1)}
{'c': 1, 'month': datetime.date(2017, 8, 1)}
{'c': 1, 'month': datetime.date(2017, 8, 1)}
為什么不呢?在{'c': 5, 'month': datetime.date(2017, 8, 1)}
總結(jié)
以上是生活随笔為你收集整理的python 按月份分组_django ORM queryset按月、周、TruncMonth分组的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python添加时间戳_在python中
- 下一篇: 黑暗之魂全系列介绍-黑暗之魂世界观名词篇