Django 等效的 SQL 不在

我有一个非常简单的查询: select * from tbl1 where title not in('asdasd', 'asdasd')

我怎么把它翻译成姜戈? 就好像我想要的是: Table.objects.filter(title__in=myListOfTitles)的反义词

73685 次浏览

try using exclude

Table.objects.exclude(title__in=myListOfTitles)
Table.objects.exclude(title__in=myListOfTitles)

(this thread is old but still could be googled)

you can use models.Q with "~" as follows:

Table.objects.filter(~Q(title__in=myListOfTitles))

this method specially is helpful when you have multiple conditions.

Django provides two options.

exclude(<condition>)
filter(~Q(<condition>))

Method 2 using Q() method

>>> from django.db.models import Q
>>> queryset = User.objects.filter(~Q(id__lt=5))