首页 > 其他 > 详细

按照月份归档

时间:2020-06-12 01:45:07      阅读:54      评论:0      收藏:0      [点我收藏+]

方法:

# django官网提供的一个orm语法
    from django.db.models.functions import TruncMonth
-官方提供
            from django.db.models.functions import TruncMonth
            Sales.objects
            .annotate(month=TruncMonth(timestamp))  # Truncate to month and add to select list
            .values(month)  # Group By month
            .annotate(c=Count(id))  # Select the count of the grouping
            .values(month, c)  # (might be redundant, haven‘t tested) select month and count
Sales就是models里面的模型类

示例:

# 按照年月统计所有的文章
    date_list = models.Article.objects.filter(blog=blog).annotate(month=TruncMonth(create_time)).values("month").annotate(count_num=Count("pk")).values_list(month,count_num)

# 先filter(blog=blog)查找到当前用户的所有文章
# annotate(month=TruncMonth(‘create_time‘)) 以创建时间的月分组
# 第二个annotate前的values("month")是分组条件

 

按照月份归档

原文:https://www.cnblogs.com/baicai37/p/13096906.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!