I am developing an app where students can evaluate their teachers. I have several models, but the important ones for this question are these:
class Professor(models.Model):
name = models.CharField(max_length=50,null=True)
categories = models.ManyToManyField(Category, related_name='professors')
def __str__(self):
return self.name
class Student(models.Model):
name = models.CharField(max_length=50,null=True)
professors = models.ManyToManyField(Professor, related_name='students',through='Studentprofesor' )
def __str__(self):
return self.name
class Studentprofesor(models.Model):
student = models.ForeignKey(Student, on_delete=models.CASCADE)
professor = models.ForeignKey(Professor, on_delete=models.CASCADE)
tested = models.BooleanField(default=False)
As far as I knew the main difference between get
and filter
was that I couldn't use get
when there were several objects with the features I was looking for. But apart from that, they worked in a similar way. get
for a single object, filter
for several objects.
However, in this case I get different results when I run get
and filter
.
if I use get
:
Student.objects.get(name="Mike").professors.all()
I obtain:
<QuerySet [<Professor: Tom>, <Professor: Jenn>]>
But if I use filter
:
Student.objects.filter(name="Mike").professors.all()
I obtain:
AttributeError: 'QuerySet' object has no attribute 'professors'
it's as if filter is not able to follow the manytomany relationship between the objects.
Why is this happening?