Django: Query using contains each value in a list
import operator
from django.db.models import Q
User.objects.filter(reduce(operator.and_, (Q(first_name__contains=x) for x in ['x', 'y', 'z'])))
for python 3
from functools import reduce
.
import operator
from django.db.models import Q
q = ['x', 'y', 'z']
query = reduce(operator.and_, (Q(first_name__contains = item) for item in q))
result = User.objects.filter(query)