How to use filtering data while using distinct method in django?

前端 未结 3 1935
后悔当初
后悔当初 2020-12-22 10:05

please help me on my problem I hope my title is enough to understand what I mean, please help me on this problem guys.

When I tried this:

id_list = g         


        
3条回答
  •  眼角桃花
    2020-12-22 10:28

    This is the best I can do for you. I think you should be selected based on the student and filtering the grades to the teacher. I also did a prefetch for all of the subjects since I can't quite tell what you need done there.

    students = Student.objects.filter(
        grades__teacher_id=teacher.id,
    ).annotate(
        total_avg=Avg('grades__Average')
    ).prefetch_related('grades__Subjects')
    

    Template:

    {% for student in students %}
          
              {{teacher}}
              {{student.grades.subjects.all}}
              {{student}}
              {{student.total_avg}}
          
    {% endfor %}
    

提交回复
热议问题