计数。Django 查询优化,Django ORM

星际尖叫32

更新:我通过以下方式解决了这个问题。视图.py

def index(request):
    # This query gives the totals for each degree in a dictionary, then i pass it into the view and loop over it to
    # render the values in Chart.js.
    degree_counts = Author.objects.order_by('degree').values('degree').annotate(Count('id'))

    # These query gives the manuscript totals for each volume.
    m_by_volume_count = Manuscript.objects.order_by('citation_volume').values('citation_volume').annotate(Count('id'))

    volumes = Manuscript.objects.all()

    # These query gives the manuscript totals for each section.
    section_count = Manuscript.objects.order_by('section__title').values('section__title').annotate(Count('id'))

    return render(
        request,
        template_name='index.html',
        context={
            'section_count': section_count,
            "m_by_volume_count": m_by_volume_count,
            "degree_counts": degree_counts,
            "manuscripts": volumes,
            "volumes": Volume.objects.all,
        }
    )

在我的模板中:

{% for b in section_count %}
{{ b.section__title }}
{% endfor %}
{% for b in section_count %}
{{ b.id__count }},
{% endfor %}

你能告诉我如何优化这些查询吗?最有效的方法是什么?我想在视图中分别呈现所有值。我附上了 django-debug-toolbar 的截图。我想学习最佳实践。

django-debug-toolbar-SQL-photo

# These queries give the totals for each degree.

def index(request):
    authors = Author.objects.all()
    author_student_count = authors.filter(degree='Student').count()
    author_bsc_count = authors.filter(degree='BSc').count()
    author_msc_count = authors.filter(degree='MSc').count()
    author_phd_count = authors.filter(degree='PhD').count()

    # These queries give the manuscript totals for each volume.

    volumes = Manuscript.objects.all()
    volume_5 = volumes.filter(citation_volume='5').count()
    volume_4 = volumes.filter(citation_volume='4').count()
    volume_3 = volumes.filter(citation_volume='3').count()
    volume_2 = volumes.filter(citation_volume='2').count()
    volume_1 = volumes.filter(citation_volume='1').count()

    # These queries give the manuscript totals for each section.

    algology_total = volumes.filter(section__title='Algology').count()
    mycology_total = volumes.filter(section__title='Mycology').count()
    geography_total = volumes.filter(section__title='Geography').count()
    entomology_total = volumes.filter(section__title='Entomology').count()
    arachnology_total = volumes.filter(section__title='Arachnology').count()
    floristics_total = volumes.filter(section__title='Floristics').count()
    mammology_total = volumes.filter(section__title='Mammology').count()


return render(
        request,
        template_name='index.html',
        context={
            "degree_counts": degree_counts,
            "authors": authors,
            "manuscripts": volumes,
            "volumes": Volume.objects.all,
            "student_count": author_student_count,
            "bsc_count": author_bsc_count,
            "msc_count": author_msc_count,
            "phd_count": author_phd_count,
            'volume_1': volume_1,
            'volume_2': volume_2,
            'volume_3': volume_3,
            'volume_4': volume_4,
            'volume_5': volume_5,
            "algology": algology_total,
            "geography": geography_total,
            "mycology": mycology_total,
            "entomology": entomology_total,
            "arachnology": arachnology_total,
            "floristics": floristics_total,
            "mammology": mammology_total,
        }
    )

我试过这样的事情:

degree_counts = Author.objects.values('degree').annotate(Count('id'))

并得到:

<QuerySet [{'degree': 'Student', 'id__count': 7}, {'degree': 'BSc', 'id__count': 1}, {'degree': 'Student', 'id__count': 1}, {'degree': 'Student', 'id__count': 4}, {'degree': 'PhD', 'id__count': 1}, {'degree': 'PhD', 'id__count': 2}, {'degree': 'PhD', 'id__count': 1}, {'degree': 'PhD', 'id__count': 1}]>
这并不能解决我的问题。正如我在文档中看到的那样,这给出了一个字典而不是原始值。我将如何在上下文中传递这些查询集(我在哪里做错了)?在我的 HTML 中,我有一个 .js 图表,其中包含 data = [ {{ author_bsc_count }}, {{ author_bsc_count }}...]

尺寸

我认为它可以帮助你

qs = Author.objects.values('degree').annotate(count=Count('degree'))

接下来您可以从中检索值 qs

Django 文档

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章