【问题标题】:Counting. Django query optmization, Django ORM数数。 Django 查询优化,Django ORM
【发布时间】:2021-06-16 12:26:28
【问题描述】:

更新:我通过以下方式解决了这个问题。 views.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 中,我有一个带有 data = [ {{ author_bsc_count }}, {{ author_bsc_count }}...] 的 .js 图表

【问题讨论】:

  • 我通过以下方式解决了这个问题:

标签: django count orm query-optimization django-orm


【解决方案1】:

我觉得可以帮到你

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

接下来你可以从qs检索值

django doc

【讨论】:

  • 嘿,你是对的,但是没有 order_by 就不行。所以我设法修复它: degree_counts = Author.objects.order_by('degree').values('degree').annotate(Count('id')) 谢谢!
  • 哦,是的,我可能忘记了。谢谢。
猜你喜欢
  • 2019-08-15
  • 2022-01-08
  • 2018-04-03
  • 2021-03-02
  • 1970-01-01
  • 2019-03-30
  • 2011-10-08
  • 2012-03-19
  • 1970-01-01
相关资源
最近更新 更多