【问题标题】:Django queryset filter based on parent of parentDjango查询集过滤器基于父级的父级
【发布时间】:2019-07-31 04:03:36
【问题描述】:

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

【问题讨论】:

  • 您是否在 Course-Model 的 Teacher 字段中分配了任何 related_name 并在 Student 模型中的 course-field 中分配了 related_name
  • 您的帖子处于什么状态。你的问题解决了吗?

标签: django django-models django-rest-framework


【解决方案1】:

teacher 是老师的 uuid/主键,那么下面的查找应该适合你

Student.objects.filter(course__teacher=teacher)

【讨论】:

    猜你喜欢
    • 2020-11-14
    • 2014-11-15
    • 2020-07-08
    • 2020-09-17
    • 2020-08-17
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多