-->

Django field lookup - exact and iexact

By: Varghese Chacko 1 year, 5 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 methodslikefilter(), exclude() and get().

As a convenience when no lookup type is provided (like in Entry.objects.get(id=10)) the lookup type is assumed to be exact.

exact

exact is used for exact match of the string with value in the field. match. If the value provided for comparison is None, it will be interpreted as an SQL NULL.

Examples:

Entry.objects.get(id__exact=14)
Entry.objects.get(id__exact=None)

SQL equivalents:

SELECT * from entry WHERE id = 14;
SELECT * from entry WHERE id IS NULL;

iexact

iexact looks for the exact string, but case-insensitive match. If the value provided for comparison is None, it will be interpreted as an SQL NULL, similar to exact.

Example:

Entry.objects.get(name__iexact='atemon entry')
Entry.objects.get(name__iexact=None)

SQL equivalents:

SELECT * from entry WHERE name ILIKE 'atemon entry';
SELECT * from entry WHERE name IS NULL;

The first query will match "ATEMON Entry", "Atemon Entry", "AtEMon ENtry" etc

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.