Grouping Django model entries by day using its datetime field

后端 未结 1 1018
春和景丽
春和景丽 2021-02-08 18:46

I\'m working with an Article like model that has a DateTimeField(auto_now_add=True) to capture the publication date (pub_date). This looks something like the following:

1条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-08 19:36

    Create an extra field that only store date data(not time) and annotate with Count:

    Article.objects.extra({'published':"date(pub_date)"}).values('published').annotate(count=Count('id'))
    

    Result will be:

    published,count
    2012-03-07,5
    2012-03-08,9
    2012-03-09,1
    

    0 讨论(0)
提交回复
热议问题