[Django]-Django equivalent of SQL not in

196👍

try using exclude

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

35👍

(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.

👤omid

28👍

Table.objects.exclude(title__in=myListOfTitles)

3👍

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))

Leave a comment