在Django ORM中执行反加入 [英] Executing Anti Join in Django ORM

查看:181
本文介绍了在Django ORM中执行反加入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个模型:

class Note(model):
    <attribs>

class Permalink(model):
    note = foreign key to Note

我想执行一个查询:获取所有没有固定链接的注释。
在SQL中,我会这样做:

I want to execute a query: get all notes which don't have a permalink. In SQL, I would do it as something like:

SELECT * FROM Note WHERE id NOT IN (SELECT note FROM Permalink); 

想知道如何在ORM中执行此操作。

Wondering how to do this in ORM.

编辑:我不想让所有的永久链接到我的应用程序。而是更愿意在DB中作为查询运行。

I don't want to get all the permalinks out into my application. Would instead prefer it to run as a query inside the DB.

推荐答案

可以使用:

 Note.objects.exclude(id__in=Permalink.objects.all().values_list('id', flat=True))

这篇关于在Django ORM中执行反加入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆