📌  相关文章
📜  如何按类别统计帖子 django - Python 代码示例

📅  最后修改于: 2022-03-11 14:47:01.642000             🧑  作者: Mango

代码示例1
#views.py
from django.db.models import Count
# You should change post in Count function based on your model.
categories = Category.objects.all().annotate(posts_count=Count('post'))

#Then you will have access to number of posts for each category:
for category in categories:
    print(category.posts_count)

#in your template
{% for category in categories %}
      

Number of posts: {{category.posts_count}}

{% endfor %}