Django中TruncMonth截取日期使用方法,按月统计
将原来的年月日按照月份来截取统计数据,具体参考如下
官方示例:
-官方提供
from django.db.models.functions import TruncMonth
Article.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
可参考示例:
# 后端代码
date_list = models.Article.objects.filter(blog=blog).annotate(month=TruncMonth('create_time')).values('month').annotate(c=Count('pk')).values('c', 'month')
<!--前端解析代码-->
{% for date in date_list %}
<p><a href="#">{{ date.month|date:'Y-m' }}({{ date.c }})</a></p>
{% endfor %}
本文来自博客园,作者:super_ip,转载请注明原文链接:https://www.cnblogs.com/superip/p/17327904.html