我想知道是否有可能使用"反向" __contains
字段查找。
https://docs.djangoproject.com/en/1.11/ref/models/querysets/#contains
例如:
class Person(models.Model):
...
last_name = models.CharField()
使用此Person
模型,我可以执行以下查询,即返回具有last_name
字段的人员,该字段适合给定的字符串:
>>> Person.objects.filter(last_name__contains='John Doe, Jane Roe, ...')
<QuerySet: [Person: Donald Doe]>
答案 0 :(得分:0)
您可以使用:
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', '...'])))
与:
相同Person.objects.exclude(Q(last_name__contains='Jhon') | Q(last_name__contains='Jane'))
您可以operator.or_
operator.and_