【问题标题】:Count all category objects in a filtered query in django?计算 django 中过滤查询中的所有类别对象?
【发布时间】:2021-04-11 23:40:54
【问题描述】:

我正在拼命地尝试计算模型中某个类别中的对象。更清楚地说:我正在抓取新闻文章并且我有这个模型:

class News(models.Model):
    title = models.CharField(max_length=2000)
    link = models.CharField(max_length=2083, default="", unique=True)
    published = models.DateTimeField()
    desc = models.CharField(max_length=2083)
    site = models.CharField(max_length=30, default="", blank=True, null=True)

这个视图过滤对象并根据请求提供服务:

class SearchResultsView(ListView):
    model = News
    template_name = 'search_results.html'
    context_object_name = 'articles'

 
    def get_queryset(self):
        query = self.request.GET.get('q')
        min_dt = self.request.GET.get('qf')
        max_dt = self.request.GET.get('qt')
        object_list = News.objects.filter(Q (title__icontains=query)| 
        Q (desc__icontains=query) & Q (published__range=(min_dt, max_dt))).order_by('-published')

我需要在我的 html django 网站上为每个网站提供多少篇文章

这是我的html

 <div class="row">
                        <table>
                            <tbody>
                             <h2>{{ articles.count }} articles found in total </h2>

                      HERE I NEED TO PUT HOW MANY ARTICLES PER SITE BELOW IS 
                      THE ACTUAL LIST ON A TABLE

                                {% for a in articles %}
                                <tr>
                                    <td>
                                        <a href="{{ a.link }}">{{ a.title }}</a>
                                    </td>
                                    <td>
                                        <p>
                                            Source: {{ a.site }}
                                        </p>
                                    </td>
                                    <td>
                                        <p>
                                            Published: {{ a.published }}
                                        </p>
                                    </td>
                                    <td>
                                        <p>
                                            More info: {{ a.desc }}
                                        </p>
                                    </td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>

换句话说,我需要按站点分组并计算结果。

谢谢!

【问题讨论】:

    标签: django templates filter count


    【解决方案1】:

    例如,如果您传递 HTML 模板的字典如下;

    { articles: object_list }
    

    您可能需要计算该列表的大小并将其作为单独的键值对添加到该字典中,然后将其与articles 数据一起读取。有两种方法可以做到这一点。您可以使用 len() python 函数或使用 Django ORM 提供的 count() 函数。请参阅下面的示例了解这两种方法及其权衡。

    使用len()

    {articles: object_list, number_of_articles: len(object_list)}
    

    这种方法效率更高,因为它避免像count() 那样再次访问该数据库。此外,使用 len() 非常适合您的方案,因为您正在以任何一种方式获取查询集。

    使用count()

    {articles: object_list, number_of_articles: object_list.count()}
    

    这有点低效,因为count() 查询将运行额外的数据库查询。仅当您不需要处理查询集并且您只需要计数时才使用count()

    因此,在您的 HTML 中,您需要获得类似的计数;

    <h2>{{ number_of_articles }} articles found in total </h2>
    

    【讨论】:

    • 感谢您这么快回复。我目前正在对所有对象进行计数,我将根据您的建议将其更改为 len,但我的问题是如何获得这些对象的 GROUP 的 len (或计数)。换句话说,按(类别)分组并计算类别结果。再次感谢您!
    • 然后您可以使用计数操作from django.db.models import Count,然后在表单中运行您的查询; object_list = News.objects.values('category').annotate(category_count=Count('category')).order_by() 这会给你一个类似的结果:object_list = [{"category": "Sports", "category_count": 2}, {"category": "Politics", "category_count": 4}]
    猜你喜欢
    • 2013-03-11
    • 1970-01-01
    • 1970-01-01
    • 2013-06-11
    • 1970-01-01
    • 1970-01-01
    • 2021-10-09
    • 1970-01-01
    • 2019-12-07
    相关资源
    最近更新 更多