【发布时间】:2016-03-19 13:02:04
【问题描述】:
在我的系统中,我有和Account 有很多Locations 的模型如下:
class Account(models.Model):
# ... also contains billing address data
class Location(models.Model):
account = models.ForeignKey('Account')
# ... also contains physical address data
我想创建一个搜索视图,允许用户根据帐单地址或实际地址搜索 Account 对象,并将结果显示在一个表中,每个关联的 Location 对象都有一个 Account 条目.我不能用Account 模型的左连接来做到这一点;这会为每个 Account 对象生成一个条目,因此不会涵盖与 Account 关联的所有 Location 对象(我不关心与帐户无关的位置)。
相反,我想通过从Location 模型到Account 模型的右连接来执行此操作。这样一来,所有帐户与其关联的每个位置都会至少包含一次,并且与帐户关联的每个位置也会被包含在内。
有没有办法在 Django 1.8+ 中做到这一点?
编辑:Account 对象不需要关联Location 对象,将来可能会出现Location.account is NULL == True 与某些Location 对象相关的情况。
【问题讨论】:
标签: django django-queryset right-join