Select values which not in another table with Django

我只是一个虾纸丫 提交于 2021-02-18 06:14:33

问题


How this SQL query can be translated to Django ORM statement?

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

Kindly help! :)

ps
table1 and table2 not bounded with ForeignKey or ManyToMany


回答1:


Using two QuerySets, as shown in the docs.

inner_qs = table2.objects.all()
results = table1.objects.exclude(field1__in=inner_qs)


来源:https://stackoverflow.com/questions/14105660/select-values-which-not-in-another-table-with-django

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!