-->

Django field lookup - contains and icontains

By: Varghese Chacko 1 year, 7 months ago

Field lookups are how we specify the conditions in the WHERE clause of an SQL. They are specified as keyword arguments for QuerySet methodslike filter(), exclude() and get().

contains

Check if the given string is contained in the value in database filed. It is translated to LIKE in SQL

Example:

Entry.objects.get(headline__contains='ATEMON')

SQL equivalent:

SELECT * from entry WHERE headline LIKE '%ATEMON%';

Note this will match the headline'ATEMON honored today' but not'atemon honored today'.

SQLite users

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

icontains

Check if the given string is contained in the value in database filed, but case-insensitive.

Example:

Entry.objects.get(headline__icontains='ATEMON')

SQL equivalent:

SELECT * from entry WHERE headline ILIKE '%ATEMON%';

SQLite users

When using the SQLite backend and non-ASCII strings, bear in mind the database note about string comparisons.

Wish to hire us?

We take the vision which comes from dreams and apply the magic of science and mathematics, adding the heritage of our profession and our knowledge to create a design.