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家公司提供的工作崗位數量顯示這些公司。
添加回答
舉報