Django Select 相关

Django Select Related

我想像这样过滤数据库。

qs = profile.objects.filter().select_related('profile_detail').select_related('location')

但这里的 location 是 profile_detail 模型中的外键。那么我该怎么做查询

class Location(models.Model):
      place = models.CharField(max_length=128)

 class ProfileDetail(models.Model):
     location = models.ForiegnKey(Location)

  class Profile(models.Model):
      detail = models.ForeignKey(ProfileDetail)

您可以使用相关的查找查询语法__

https://docs.djangoproject.com/en/1.10/topics/db/queries/#lookups-that-span-relationships

qs = profile.objects.filter().select_related('detail__location')

它应该 detail 而不是 profile_detail。正如您在 Profile 中的字段称为