How to query Case-insensitive data in Django ORM?
Asked Answered
L

1

312

How can I query/filter in Django and ignore the cases of my query-string?

I've got something like and like to ignore the case of my_parameter:

MyClass.objects.filter(name=my_parameter)
Ligialignaloes answered 31/7, 2012 at 15:8 Comment(0)
L
586

I solved it like this:

MyClass.objects.filter(name__iexact=my_parameter)

There is even a way to use it for substring search:

MyClass.objects.filter(name__icontains=my_parameter)

There's a link to the documentation.

Ligialignaloes answered 31/7, 2012 at 15:8 Comment(6)
Would you please explain what "substring search" means?Spectra
@RishabhAgrahari You search inside a string, for example you search for "beer" and you get the matches "beer" and "beers".Ligialignaloes
And for example a match with "there is beer in my stomach".Lobar
doc for iexact: docs.djangoproject.com/en/dev/ref/models/querysets/#iexactLaquanda
It is sql likeHindenburg
Well done sir, this has given me like 3 hours of trouble :)Dysphemism

© 2022 - 2024 — McMap. All rights reserved.