I have a list of names, e.g.:
name_list = [\'Alpha\', \'bEtA\', \'omegA\']
Currently I have the following queryset:
MyModel
Here is an example using a list comprehension. It is similar to catherine's answer, but with one database hit like Derek Kwok's answer (but procedural instead of functional).
q_list = Q()
for q in [Q(name__iexact=n) for n in name_list]:
q_list |= q
MyModel.objects.filter(q_list)
For those that like to avoid zip, map, reduce, and such in Python.
Here's my solution, which uses Q objects instead:
name_list = ['Alpha', 'bEtA', 'omegA']
q_list = map(lambda n: Q(name__iexact=n), name_list)
q_list = reduce(lambda a, b: a | b, q_list)
MyModel.objects.filter(q_list)
name_list = ['Alpha', 'bEtA', 'omegA']
results = MyModel.objects.none()
for name in name_list:
results |= MyModel.objects.filter(name__iexact=name)
Ok I test it and it works :)