如何将此SQL查询转换为Django ORM语句?
SELECT field1, field2, field3
FROM table1
WHERE field1 NOT IN
(SELECT 2_field1 FROM table2);
请帮忙! :)
PS
table1
和table2
未与ForeignKey
或ManyToMany
答案 0 :(得分:10)
使用两个QuerySet,如图in the docs所示。
inner_qs = table2.objects.all()
results = table1.objects.exclude(field1__in=inner_qs)