更新:我通过以下方式解决了这个问题。视图.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}]>
我认为它可以帮助你
qs = Author.objects.values('degree').annotate(count=Count('degree'))
接下来您可以从中检索值 qs
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句