Django three tables Association query

class School(models.Model):
    name = models.CharField(max_length=10)

class Claxx(models.Model):
    name = models.CharField(max_length=10)
    school = models.ForeignKey(School, related_name="claxxes")

class Student(models.Model):
    name = models.CharField(max_length=10)
    claxx = models.ForeignKey(Claxx, related_name="stues")

    def __str__(self):
        return self.name

School-Class-Student, three tables

Peking University

1
        ,
2
                            

how to query "all the students in Peking University"?

my simple idea is to first query all the classes below Peking University, and then traverse each class to get all the students

is there any other way, such as annotate or aggregate, can you write it out?

Please take a look at it

Jul.05,2022

Student.objects.filter(claxx__school__name='').distinct()
Menu