2 回答

TA贡献1862条经验 获得超6个赞
您可以在查询集上使用 Django 的聚合来实现此目的。在你做这样的事情:CountViews.py
from django.db.models import Count
queryset = MyModel.objects.all().annotate(count = Count('Google'))
dict1= {}
for each in queryset:
#print(each.my_charfield, each.count)
context[each.my_charfield] = each.count
return render(request, 'some.html', context=dict1)
在模板中访问它像
{% for key, value in dict1.items %}
{{key}} ({{value}})
{% endfor %}

TA贡献1744条经验 获得超4个赞
在 views.py 文件中:
def company(request):
comp_details = Company.objects.annotate(jobs=Count('job')).order_by('-jobs')[:10]
return render(request, 'index.html', {'comp_details': comp_details})
在索引.html文件中:
{% for company in comp_details %}
{{ company.name }}
{% endfor %}
这将根据前10家公司提供的工作岗位数量显示这些公司。
添加回答
举报