Search code examples
djangosqlitedjango-modelsdjango-databasedjango-model-field

Is django model field lookup_type 'contains' case-sensitive or insensitive..?


As per documentation 'contains' field lookup is case-sensitive and 'icontains' is case-Insensitive, but I don't see any difference while i'm querying it.

>>> from users.models import SnetUser
>>> SnetUser.objects.get(email__contains='Satti')
<SnetUser: satti>
>>> SnetUser.objects.get(email__contains='satti')
<SnetUser: satti>
>>> obj = SnetUser.objects.get(email__contains='satti')
>>> obj.email
'[email protected]'

Both are resulting same.

Note: I'm using django's SQLite DB locally


Solution

  • This case is written in the docs.

    contains field lookup is being converted to LIKE clause and in SQLite it is case-insensitive.

    In case you want to make LIKE clause to work case-sensitively, you need to use the following PRAGMA:

    PRAGMA case_sensitive_like = true;

    >>> from django.db import connection
    
    >>> with connection.cursor() as cursor:
    ...     cursor.execute('PRAGMA case_sensitive_like = true;')
    
    >>> SnetUser.objects.get(email__contains='Satti')
    <QuerySet []>