Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making Django model Query case-insensitive

I would like to look up users by their email without worrying about uppercase letters.

How would I do this? I tried

customer = Customer.objects.get(email__lower="[email protected]")

and got this error.

django.core.exceptions.FieldError: Unsupported lookup 'lower' for EmailField or join on the field not permitted.
like image 637
Jake Mulhern Avatar asked Jan 21 '26 17:01

Jake Mulhern


1 Answers

You can work with the __iexact lookup [Django-doc]:

customer = Customer.objects.get(email__iexact='[email protected]')

Contrary to popular belief, calling lowercase over two items does not check if the two match in a case insensitive way. Some characters have no lowercase/uppercase variant, for example ß [wiki]. In order to determine if two strings match case-insensitive, one should apply a case folding [wiki].

like image 120
Willem Van Onsem Avatar answered Jan 23 '26 16:01

Willem Van Onsem



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!