使 Django 模型查询不区分大小写

Making Django model Query case-insensitive

我想通过电子邮件查找用户而不用担心大写字母。

我该怎么做?我试过了

customer = Customer.objects.get(email__lower="test@gmail.com")

遇到这个错误。

django.core.exceptions.FieldError: Unsupported lookup 'lower' for EmailField or join on the field not permitted.

您可以使用 __iexact lookup [Django-doc]:

customer = Customer.objects.get(<strong>email__iexact='test@gmail.com'</strong>)

与流行的看法相反,对两个项目调用小写 不会 检查两者是否以不区分大小写的方式匹配。有些字符没有 lowercase/uppercase 变体,例如 ß [wiki]. In order to determine if two strings match case-insensitive, one should apply a case folding [wiki].