38👍
✅
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)
5👍
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 🙂
- [Django]-Django REST Framework (DRF): Set current user id as field value
- [Django]-Django models avoid duplicates
- [Django]-Django Multiple Authentication Backend for one project
2👍
How about annotating the target record with the transformed name as lower case? Should reduce the number of conditions required considerably, boosting performance…
from django.db.models.functions import Lower
lcase_name_list = [x.lower() for x in name_list]
MyModel.objects.annotate(lcase_name=Lower('name').filter(lcase_name__in=lcase_name_list)
- [Django]-Sending post data from angularjs to django as JSON and not as raw content
- [Django]-Django: guidelines for speeding up template rendering performance
- [Django]-What does 'many = True' do in Django Rest FrameWork?
1👍
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.
- [Django]-Django models avoid duplicates
- [Django]-AccessDenied when calling the CreateMultipartUpload operation in Django using django-storages and boto3
- [Django]-Images from ImageField in Django don't load in template
Source:stackexchange.com