9

如何将此 SQL 查询转换为 Django ORM 语句?

SELECT field1, field2, field3
FROM table1
WHERE field1 NOT IN 
(SELECT 2_field1 FROM table2);

请帮忙!:)

ps
table1并且table2不受ForeignKeyor限制ManyToMany

4

1 回答 1

18

使用两个 QuerySet,如文档中所示。

inner_qs = table2.objects.all()
results = table1.objects.exclude(field1__in=inner_qs)
于 2012-12-31T20:16:12.163 回答