0

我想像这样过滤数据库。

qs = profile.objects.filter().select_related('profile_detail').select_related('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)
4

1 回答 1

2

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

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一样

于 2016-10-20T10:37:43.987 回答