[Answered ]-Django generic ListView, refering to each items in the object_list

1👍

You can annotate the queryset with the number of Trainees:

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 Trainees.

In the template you thus can render this with:

{% for training in trainings %}
    {{ trailing }}: {{ trailing.headcount }}
{% endfor %}

Leave a comment