Django 进行涉及一对多对一关系的双重外键查找

Django make a double foreign key lookup involving one to many to one relations

我有以下型号:

def Order(models.Model):
    receiver = models.ForeignKey(Receiver)
    warehouse = models.ForeignKey(Warehouse)

def Receiver(models.Model):
    user = models.ForeignKey(User) #this is not made one to one because user can have more than one receiver
    name = ...
    zipcode = ...

def Warehouse(models.Model):
    city = ...
    street = ...

我想要 select 与 request.User 对象相关的所有 Warehouse 条目。我现在能做到的唯一方法是:

orders = Order.objects.filter(receiver__user=request.User)
# here i set orders warehouse ids to list called ids
user_warehouses = Warehouse.objects.filter(pk__in=ids)

但我有一种强烈的感觉,我正在发明轮子。有更简单的 Django 方式吗?

Warehouse.objects.filter(order__receiver__user=request.user)

您可以向后遍历关系 ("reverse lookup") 并使用双下划线语法遍历多个级别

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