User.objects.filter(name__in=["A", "AB", "a", "ab", "Ab"])
This is an example of using in
field lookup in Django
.
But I want to know that is here any trick of using in
as case insensitive like icontains
?
Thank you
User.objects.filter(name__in=["A", "AB", "a", "ab", "Ab"])
This is an example of using in
field lookup in Django
.
But I want to know that is here any trick of using in
as case insensitive like icontains
?
Thank you
Others' answers are excellent.
I looked for another way that use Lower function
lower_list = map(lambda x:x.lower(), ["A", "AB", "a", "ab", "Ab"])
User.objects.annotate(name_lower=Lower('name').filter(name_lower__in=lower_list)
It's just another way. I think that other people's answers are close to a good answer.
For case insensitive operation you can use iregex
User.objects.filter(name__iregex=r'(A|AB|a|ab|Ab)')
Ref: https://docs.djangoproject.com/en/dev/ref/models/querysets/#iregex
Update: You can make own lookup. Get help from https://docs.djangoproject.com/en/dev/ref/models/lookups/
According to Django query case-insensitive list match there is no solution other then do iregex
lookup