基于父级的 Django 查询集过滤器

Mic*_*Sun 3 django django-models django-rest-framework

假设我有模型教师、课程、学生。Student 模型有一个foreignKey 一对多到Course,Course 有一个foreignKey 一对多到Teacher。当获取所有 Student 对象的查询集时,有没有办法根据教师查询它们?

Sha*_*kil 8

teacher是老师的 uuid/primary key 然后下面的查找应该适合你

Student.objects.filter(course__teacher=teacher)
Run Code Online (Sandbox Code Playgroud)