【问题标题】:Django select related of select related [duplicate]Django选择相关的选择相关[重复]
【发布时间】:2020-10-01 08:01:01
【问题描述】:

我有一个 Student 模型,它有一个 School 模型(相关名称是 school)的外键,它本身有一个 Country 的 FK 模型(相关名称为country)。

我想选择学生及其学校和国家。 我需要这样写吗:

student = Student.objects.filter(pk=123).select_related("school", "school__country").first()
student.school # use object cache
student.school.country # use object cache

或者这样就够了:

student = Student.objects.filter(pk=123).select_related("school__country").first()

【问题讨论】:

    标签: django django-orm


    【解决方案1】:

    不,school__country 版本意味着还需要选择 school

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-21
    • 2018-12-22
    • 1970-01-01
    • 1970-01-01
    • 2020-05-09
    • 1970-01-01
    • 2020-02-01
    相关资源
    最近更新 更多