I am trying to access names of all the students having marks 100.
def get_names(request):
students = Result.objects.filter(marks=100)
student_name = []
for x in students:
student_name.append(x.student)
for x in student_name:
print (x.name)
return render(request, 'exam/index.html', {'names': student_name})
When I run this view, 'Student has no attribute '__getitem__'' is displayed on the line print(x.name). I read something about dynamic object filter but cannot figure it out in this case.
Model structure
class Student(models.Model):
name = models.CharField(max_length=20)
stream = models.CharField(max_length=20)
class Result(models.Model):
student = models.ForeignKey(Student, on_delete=models.CASCADE)
marks = models.IntegerField()