B
B
bituke2021-11-13 17:40:44
Django
bituke, 2021-11-13 17:40:44

How to get the number of students of all courses of the current teacher?

there is a course model, the course has a teacher (foreignkey) and students (manytomanyfield)
the task is to get the number of students from all courses of a particular teacher. How easy and simple is it to implement? Or do you have to loop through?
Describe with an example, please)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
albert-gaynullin, 2021-11-16
@bituke

Try this code to count the number of teacher's students

teacher_student_count = User. courses.all().aggregate(Count('students')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question