1👍
✅
You can annotate the queryset with the number of Trainee
s:
from django.db.models import Count
class TrainingsView(ListView):
model = NewHireTraining
template_name = 'nht/trainings.html'
context_object_name = 'trainings'
queryset = NewHireTraining.objects.annotate(headcount=Count('trainee'))
The NewHireTraining
objects that arise from this queryset will have an extra attribute .headcount
with the total number of related Trainee
s.
In the template you thus can render this with:
{% for training in trainings %}
{{ trailing }}: {{ trailing.headcount }}
{% endfor %}
Source:stackexchange.com