Michael Sun
Michael Sun

Reputation: 189

Django queryset filter based on parent of parent

Say I have the models Teacher, Course, Student. Student model has a foreignKey one-to-many- to Course, and Course has a foreignKey one-to-many to Teacher. When getting a queryset of all Student objects, is there a way to query them based on Teacher?

Upvotes: 3

Views: 1214

Answers (1)

Shakil
Shakil

Reputation: 4630

Say teacher is a teacher's uuid/primary key then following look-up should work for you

Student.objects.filter(course__teacher=teacher)

Upvotes: 8

Related Questions