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 = 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:
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
this is my desire answer
this is how I post in html
views.py
return render(request, 'Homepage/index.html', {"piste":piste})
html
{% for n in piste %}
<tr>
<td>{{n.Teacher}}</td>
<td>{{n.Subjects}}</td>
<td>{{n.Students_Enrollment_Records.Students_Enrollment_Records.Student_Users}}</td>
<td>{{n}}</td>
</tr>
{% endfor %}
This is model.py
class EmployeeUser(models.Model):
Image = models.ImageField(upload_to='images', null=True, blank=True)
Employee_Number = models.CharField(max_length=500, null=True)
Username = models.CharField(max_length=500, null=True)
Password = models.CharField(max_length=500, null=True)
My_Department = models.ForeignKey(Department, related_name='+', on_delete=models.CASCADE, blank=True)
My_Position = models.ForeignKey(Position, related_name='+', on_delete=models.CASCADE, blank=True)
Firstname = models.CharField(max_length=500, null=True)
Middle_Initial = models.CharField(max_length=500, null=True)
Lastname = models.CharField(max_length=500, null=True)
Educational_Attainment = models.ForeignKey(EducationalAttainment, related_name='+', on_delete=models.CASCADE,
null=True)
Landline = models.CharField(max_length=500, null=True)
Mobile_Number = models.CharField(max_length=500, null=True)
Email = models.CharField(max_length=500, null=True)
Facebook_Acoount = models.CharField(max_length=500, null=True)
Fathers_Firstname = models.CharField(max_length=500, null=True)
Fathers_Middle_Initial = models.CharField(max_length=500, null=True)
Fathers_Lastname = models.CharField(max_length=500, )
Educational_Attainment_Father = models.ForeignKey(EducationalAttainment, related_name='+', on_delete=models.CASCADE,
null=True)
Father_Occupation = models.CharField(max_length=500, null=True)
Father_Company_Employed = models.CharField(max_length=500, null=True)
Father_Landline = models.CharField(max_length=500, null=True)
Father_MobileNo = models.CharField(max_length=500, null=True)
Father_Email = models.CharField(max_length=500, null=True)
Father_Facebook_Account = models.CharField(max_length=500, null=True)
Mother_FirstName = models.CharField(max_length=500, null=True)
Mother_Middle_Initial = models.CharField(max_length=500, null=True)
Mother_Maiden_LastName = models.CharField(max_length=500, null=True)
Educational_AttainmentID_Mother = models.ForeignKey(EducationalAttainment, related_name='+',
on_delete=models.CASCADE, null=True)
Mother_Occupation = models.CharField(max_length=500, null=True)
Mother_Company_Employed = models.CharField(max_length=500, null=True)
Mother_Landline = models.CharField(max_length=500, null=True)
Mother_MobileNo = models.CharField(max_length=500, null=True)
Mother_Email = models.CharField(max_length=500, null=True)
Mother_Facebook_Account = models.CharField(max_length=500, null=True)
Family_Status = models.ForeignKey(FamilyStatu, related_name='+', on_delete=models.CASCADE, null=True)
Country = models.CharField(max_length=500, null=True)
ZIP_Postal_Code = models.CharField(max_length=500, null=True)
State_Province = models.CharField(max_length=500, null=True)
City = models.CharField(max_length=500, null=True)
Barangay = models.CharField(max_length=500, null=True)
Unit_Number_Street = models.CharField(max_length=500, null=True)
LandMark = models.CharField(max_length=500, null=True)
AddressLine1 = models.CharField(max_length=500, null=True)
AddressLine2 = models.CharField(max_length=500, null=True)
AddressLine3 = models.CharField(max_length=500, null=True)
def __str__(self):
suser = '{0.Firstname} {0.Middle_Initial} {0.Lastname}'
return suser.format(self)
class StudentsEnrolledSubject(models.Model):
Students_Enrollment_Records = models.ForeignKey(StudentsEnrollmentRecord, related_name='+',
on_delete=models.CASCADE, null=True)
Subject_Section_Teacher = models.ForeignKey(SubjectSectionTeacher, related_name='+', on_delete=models.CASCADE,
null=True)
this is the full view.py
m = EmployeeUser.objects.get(Username=request.POST['p_user'], Password = request.POST['p_pass'], My_Position =request.POST['position'])
piste = grade.objects.all().filter(Teacher=m.id).values_list('Students_Enrollment_Records').annotate(Average=Avg('Average')).order_by('Grading_Categories').distinct()
return render(request, 'Homepage/index.html', {"piste":piste})
UPDATE
when i tried this answer of mr @nigel222
piste = grade.objects.filter(Teacher=m.id).annotate(grade_average=Avg('Average')).order_by('Grading_Categories').distinct()
and to my html
{% for n in piste %}
<tr>
<td>{{n.Teacher}}</td> <!-- 1 -->
<td>{{n.Subjects}}</td> <!-- 2 -->
<td>{{n.Students_Enrollment_Records.Students_Enrollment_Records.Student_Users}}
</td> <!-- 3 -->
<td>{{n}}</td> <!--4 -->
</tr>
{% endfor %}
i get this result



