博客分类统计

博客类型模型

class BlogType(models.Model):
type_name = models.CharField(max_length=15)

def __str__(self):
return self.type_name

第一种方法

#获取博客分类的对应博客数量
blog_types = BlogType.objects.all() #所有博客类型
blog_types_list = []
for blog_type in blog_types:
blog_type.blog_count = Blog.objects.filter(blog_type=blog_type).count() #统计每种类型的博客数量,给博客类型新加一个属性
blog_types_list.append(blog_type) #保存到列表中

context = {}
context['blog_types'] = blog_types_list
{% for blog_type in blog_types %}
<li>
<a href="{% url 'blogs_with_type' blog_type.pk %}">
{{ blog_type.type_name }}({{ blog_type.blog_count }})
</a>
</li>
{% empty %}
<li>暂无分类</li>
{% endfor %}
前端页面可以调用新加的属性。

第二种方法
annotate注释,使用annotate拓展查询字段,就是给遍历出来模型对象写一条备注之类的信息
from django.db.models import Count
context['blog_types'] = BlogType.objects.annotate(blog_count=Count('blog'))
count中写与BlogType关联的模型的类名的小写。实现的结果与上面一种方法一样,但是原理不同。
感觉很像依据博客类型对博客进行统计



日期归档统计
Blog.objects.dates('created_time', 'month', order='DESC')#返回日期数据

<QuerySet [datetime.date(2020, 10, 1)]>
#获取日期归档对应的博客数量
blog_dates = Blog.objects.dates('created_time', 'month', order='DESC') #获取每月日期数据
blog_dates_dict = {} #创建空字典
for blog_date in blog_dates:
blog_count = Blog.objects.filter(created_time__year=blog_date.year,created_time__month=blog_date.month).count() #统计对应日期的博客数量
blog_dates_dict[blog_date] = blog_count #日期为键,数量为值。保持在字典中

context = {}
context['blog_dates'] = blog_dates_dict

前端
{% for blog_date,blog_count in blog_dates.items %}
<li>
<a href="{% url 'blogs_with_date' blog_date.year blog_date.month %}">
{{ blog_date|date:"Y年m月" }}({{ blog_count }})
</a>
</li>
{% endfor %}

字典.items遍历键和值。

第二种

>>> from django.db.models import Count
>>> Blog.objects.dates('created_time', 'month', order='DESC')
<QuerySet [datetime.date(2020, 10, 1)]>
>>> Blog.objects.dates('created_time', 'month', order='DESC').annotate(blog_count=Count('created_time'))
<QuerySet [datetime.date(2020, 10, 1), 34]>
>>> ds1=Blog.objects.dates('created_time', 'month', order='DESC').annotate(blog_count=Count('created_time'))
>>> ds1[0]
datetime.date(2020, 10, 1)
>>> ds1[1]
Traceback (most recent call last):

>>> ds2=list(ds1)
>>> ds2
[datetime.date(2020, 10, 1), 34]
>>> ds2[1]
34

在views中通过切片把对应的值放到字典中,再传回前端页面也可行,不过比第一种方法看起来复杂。



原文地址:https://www.cnblogs.com/lag1/p/13823946.html