Print correctly 2 models in my template

Hello dear all.

I would like to submit the following difficulty to you. I am in the final stages of a django application and I would like to generate a statistical report mainly from 2 tables.
Student (name, first name, sex, class)
Class (code, label)
my report is presented as follows:
in column: class - girl - boy - total)
at row level, we must be able to display each of the classes with the number of girls, the number of boys and the total of students.
I manage to display each of the classes on the pdf report but I crash on the correct request to write in my view for the template in order to distinctly display the number of girls per class, boys and the total.

Could you help me the experts?

Can you please post code snippets? You can edit your original post and add code to it using the ``` marks.

As Suttonium describes, it’s best if you can share some code, especially what you’re doing in your view and what kind of view (e. g. if it’s class-based or function-based) you’re using.

From what you’ve written so far though it sounds like you could have use of the Django docs on aggregation. It seems like you’d want something like:

from django.db.models import Count, Q
num_girls = Count('student', filter=Q(student__sex='female')
num_boys = Count('student', filter=Q(student__sex='male')
num_total = Count('student')
classes = SchoolClass.objects.annotate(num_girls=num_girls).annotate(num_boys=num_boys).annotate(num_total=num_total)
context = {'classes': classes}
# ...

If I understand things correctly (I literally just learned how to use .annotate), something like that should give you a queryset you can loop over, with each object/schoolclass having attributes .num_girls (for count of girls in the schoolclass), .num_boys and .num_total. If I’m not mistaken, these should be possible to use in the template just like other attributes. If someone knows this to be an inefficient solution to the problem then please describe that.