This document has the API references of lookups, the Django API for building the
WHERE clause of a database query. To learn how to use lookups, see Making queries; to learn how to create new lookups, see Custom Lookups.
Django has two base classes that follow the query expression API and from where all Django builtin lookups are derived:
A lookup expression consists of three parts:
__icontains) that, if omitted, defaults to
Django uses RegisterLookupMixin to give a class the interface to register lookups on itself. The two prominent examples are Field, the base class of all model fields, and Transform, the base class of all Django transforms.
A mixin that implements the lookup API on a class.
Registers a new lookup in the class. For example
DateField.register_lookup(YearExact) will register
YearExact lookup on
DateField. It overrides a lookup that already exists with the same name.
lookup_name will be used for this lookup if provided, otherwise
lookup.lookup_name will be used.
Returns the Lookup named
lookup_name registered in the class. The default implementation looks recursively on all parent classes and checks if any has a registered lookup named
lookup_name, returning the first match.
Returns a dictionary of each lookup name registered in the class mapped to the Lookup class.
Returns a Transform named
transform_name. The default implementation looks recursively on all parent classes to check if any has the registered transform named
transform_name, returning the first match.
The query expression API is a common set of methods that classes define to be usable in query expressions to translate themselves into SQL expressions. Direct field references, aggregates, and
Transform are examples that follow this API. A class is said to follow the query expression API when it implements the following methods:
Generates the SQL fragment for the expression. Returns a tuple
(sql, params), where
sql is the SQL string, and
params is the list or tuple of query parameters. The
compiler is an
SQLCompiler object, which has a
compile() method that can be used to compile other expressions. The
connection is the connection used to execute the query.
expression.as_sql() is usually incorrect - instead
compiler.compile(expression) should be used. The
compiler.compile() method will take care of calling vendor-specific methods of the expression.
Custom keyword arguments may be defined on this method if it’s likely that
as_vendorname() methods or subclasses will need to supply data to override the generation of the SQL string. See Func.as_sql() for example usage.
as_sql()method. When an expression is compiled by
compiler.compile(), Django will first try to call
vendornameis the vendor name of the backend used for executing the query. The
vendornameis one of
mysqlfor Django’s built-in backends.
lookup_name. For instance, by returning
transform_name. For instance, by returning
get_lookup()method. It must be a Field instance.
Transform is a generic class to implement field transformations. A prominent example is
__year that transforms a
DateField into a
The notation to use a
Transform in a lookup expression is
This class follows the Query Expression API, which implies that you can use
____. It’s a specialized Func() expression that only accepts one argument. It can also be used on the right hand side of a filter or directly as an annotation.
A boolean indicating whether this transformation should apply to both
rhs. Bilateral transformations will be applied to
rhs in the same order as they appear in the lookup expression. By default it is set to
False. For example usage, see Custom Lookups.
The left-hand side - what is being transformed. It must follow the Query Expression API.
The name of the lookup, used for identifying it on parsing query expressions. It cannot contain the string
Defines the class this transformation outputs. It must be a Field instance. By default is the same as its
Lookup is a generic class to implement lookups. A lookup is a query expression with a left-hand side, lhs; a right-hand side, rhs; and a
lookup_name that is used to produce a boolean comparison between
rhs such as
lhs in rhs or
lhs > rhs.
The notation to use a lookup in an expression is
This class acts as a query expression, but, since it has
= on its construction, lookups must always be the end of a lookup expression.
The left-hand side - what is being looked up. The object must follow the Query Expression API.
The right-hand side - what
lhs is being compared against. It can be a plain value, or something that compiles into SQL, typically an
F() object or a
The name of this lookup, used to identify it on parsing query expressions. It cannot contain the string
Returns a tuple
(lhs_string, lhs_params), as returned by
compiler.compile(lhs). This method can be overridden to tune how the
lhs is processed.
compiler is an
SQLCompiler object, to be used like
compiler.compile(lhs) for compiling
connection can be used for compiling vendor specific SQL. If
lhs is not
None, use it as the processed
lhs instead of