简体   繁体   中英

Python contains and icontains both return case insensitive results

I have a very curious case that I'm not sure I understand. I am filtering data using field__contains but am getting case insensitive results. As an example:

Entry Data:

{
    "id": 2,
    "last_login": null,
    "is_superuser": false,
    "username": "slappy",
    "first_name": "don",
    "last_name": "sweeney",
    "email": "don@bruins.com",
    "is_staff": false,
    "is_active": true,
    "date_joined": "2019-12-09T09:07:11.897502Z",
    "groups": [],
    "user_permissions": []
}

Search:

query = { 'email__contains' : 'DON' }
data_set = data_set.filter(Q(**query))
return data_set

The results return the entry despite the casing clearly being wrong. I checked other filter types and they are working as expected:

Search 2:

query = { 'username__exact' : 'SLAPPY' }
data_set = data_set.filter(Q(**query))
return data_set

This search produces no results, as would be expected.

Am I missing something here? Would could be the root cause of this? I would expect contains to be case sensitive and icontains to be case insensitive, otherwise why would the two exist?

I think that information has been given with documentation of contains :

SQLite users

SQLite doesn't support case-sensitive LIKE statements; contains acts like icontains for SQLite. See the database note for more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM