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

12 views
Skip to first unread message

kaito

unread,
Jan 16, 2020, 8:06:39 AM1/16/20
to Django users
please visit this site for more info https://stackoverflow.com/questions/59764028/how-to-use-filtering-data-while-using-distinct-method-in-django

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

data.PNG



When I tried this:

   "" id_list = grade.objects.filter(Teacher=m.id).values_list('Students_Enrollment_Records_id',flat=True).distinct() ""


I use `distinct()` to eliminates duplicate rows of Students Enrollment Record from the query results but I wonder why the result is like this:

badresult.PNG





What should I do to show the Students name not that QuerySet in my html?

This is my current **views.py**:

    id_list = grade.objects.filter(Teacher=m.id).values_list('Students_Enrollment_Records_id',flat=True).distinct()
    print(id_list)
    grades = grade.objects.filter(Students_Enrollment_Records_id__in=id_list)
    print(grades)

This is my **models.py**:

    class grade(models.Model):
        Teacher = models.ForeignKey(EmployeeUser, related_name='+', on_delete=models.CASCADE,
                                    null=True, blank=True)
        Grading_Categories = models.ForeignKey(gradingCategories, related_name='+', on_delete=models.CASCADE,
                                               null=True, blank=True)
        Subjects = models.ForeignKey(Subject, related_name='+', on_delete=models.CASCADE, null=True)
        Students_Enrollment_Records = models.ForeignKey(StudentsEnrolledSubject, related_name='+',
                                                        on_delete=models.CASCADE, null=True)
        Average = models.FloatField(null=True, blank=True)

**UPDATE**

when I tried this 

    piste = grade.objects.filter(Teacher_id=m.id).values_list('Students_Enrollment_Records').annotate(Average=Avg('Average')).order_by('Grading_Categories').distinct()

the computation is fix but the teacher name, Subject and Name of students didn't display but the ID is display just like this 

result.PNG


this is my desire answer 

desireanswer.png





Reply all
Reply to author
Forward
0 new messages