Reputation: 134
I'm wondering if it is somehow possible to use "the inverse" of the __contains
fields lookup.
https://docs.djangoproject.com/en/1.11/ref/models/querysets/#contains
So for example:
class Person(models.Model):
...
last_name = models.CharField()
And with this Person
model I could do for example the following query that'll return the Persons that have a last_name
field that would fit in the string given:
>>> Person.objects.filter(last_name__contains='John Doe, Jane Roe, ...')
<QuerySet: [Person: Donald Doe]>
Upvotes: 0
Views: 358
Reputation: 4213
You can use:
import operator
from functools import reduce
from django.db.models import Q
Person.objects.exclude(reduce(operator.or_, (Q(last_name__contains=x) for x in ['John Doe', 'Jane Roe', '...'])))
is the same than:
Person.objects.exclude(Q(last_name__contains='Jhon') | Q(last_name__contains='Jane'))
you can change operator.or_
by operator.and_
Upvotes: 0