Reputation:
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
Upvotes: 0
Views: 152
Reputation: 13731
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 %}
<tr>
<td>{{teacher}}</td>
<td>{{student.grades.subjects.all}}</td>
<td>{{student}}</td>
<td>{{student.total_avg}}</td>
</tr>
{% endfor %}
Upvotes: 0
Reputation: 8202
You don't want values_list
(which is the data you are getting in column 4). You want a queryset of grade
objects:
piste = grade.objects.filter(Teacher_id=m.id
).annotate(Average=Avg('Average')
).order_by('Grading_Categories'
).distinct()
and then in your template, something like
{% 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 %}
1 and 2 render EmployeeUser
and Subject
objects. 1 will return the __str__
representation of EmployeeUser
which ought to be OK. Alternatively you can explicitly use {{n.Teacher.FirstName}}
etc. in your template.
3 I don't understand, because you don't show the Students_Enrolled_Subject
model.
4 is now wrong. Perhaps you want the annotation {{n.Average}}
?
Please, as soon as possible, learn to use Django/ Python coding conventions. Model
subclasses (and Python class names in general) start with a capital letter. Instances of classes are lowercase. Fieldnames/attributes are normally lower_case and definitely start with a lowercase letter. Model subclass names are a singular name not a plural name. Not doing this is horribly confusing to any experiemced Django coder. So,
class Grade(models.Model):
teacher = models.ForeignKey(EmployeeUser, related_name='+', on_delete=models.CASCADE,
null=True, blank=True)
grading_categories = models.ForeignKey(GradingCategory, related_name='+', on_delete=models.CASCADE,
null=True, blank=True)
subject = 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)
Upvotes: 0
Reputation: 3158
I haven't worked with Django much for a couple of years, but this is what I think is happening.
You're assigning a values_list (a tuple) to piste
. You're not assigning grade
objects to piste
. However, in your template you're expecting the elements of piste
to be grades.
I believe you need to get the grade
object first and send that to the template as well as the piste
.
Upvotes: 1