Django 中的外连接建模
我有一个多对多关系表,其中连接基础中的一些数据
是我的模型的基本版本,如下所示:
class FooLine(models.Model):
name = models.CharField(max_length=255)
class FooCol(models.Model):
name = models.CharField(max_length=255)
class FooVal(models.Model):
value = models.CharField(max_length=255)
line = models.ForeignKey(FooLine)
col = models.ForeignKey(FooCol)
我试图搜索特定行的每个值,如果该值不存在,则为空(基本上我是尝试显示 fooval 表,其中未填充的值为空值) 典型的 sql 有
SELECT value FROM FooCol LEFT OUTER JOIN
(FooVal JOIN FooLine
ON FooVal.line_id == FooLine.id AND FooLine.name = "FIXME")
ON FooCol.id = col_id;
没有办法使用 django 模型对上述查询进行建模
谢谢
I have a many to many relationship table whith some datas in the jointing base
a basic version of my model look like:
class FooLine(models.Model):
name = models.CharField(max_length=255)
class FooCol(models.Model):
name = models.CharField(max_length=255)
class FooVal(models.Model):
value = models.CharField(max_length=255)
line = models.ForeignKey(FooLine)
col = models.ForeignKey(FooCol)
I'm trying to search every values for a certain line with a null if the value is not present (basically i'm trying to display the fooval table with null values for values that haven't been filled)
a typical sql would be
SELECT value FROM FooCol LEFT OUTER JOIN
(FooVal JOIN FooLine
ON FooVal.line_id == FooLine.id AND FooLine.name = "FIXME")
ON FooCol.id = col_id;
Is there any way to modelise above query using django model
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
外连接可以被视为一种 hack,因为 SQL 缺乏“导航”。
您所拥有的是一个简单的 if 语句情况。
这就是外连接的真正含义——尝试使用 NULL 替换进行查找。
唯一的优化如下所示。
Outer joins can be viewed as a hack because SQL lacks "navigation".
What you have is a simple if-statement situation.
That's what an outer join really is -- an attempted lookup with a NULL replacement.
The only optimization is something like the following.