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 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
Aggregate, the base class of all Django aggregates.

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 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:

Responsible for producing the query string and parameters for the expression.
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.

Calling 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.

Works like as_sql() method. When an expression is compiled by
compiler.compile(), Django will first try to call as_vendorname(),
where vendorname is the vendor name of the backend used for executing
the query. The vendorname is one of postgresql, oracle,
sqlite, or mysql for Django’s built-in backends.

A boolean indicating whether this transformation should apply to both
lhs and 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.

A 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 lhs and rhs such as lhsinrhs or
lhs>rhs.

The notation to use a lookup in an expression is
<lhs>__<lookup_name>=<rhs>.

This class doesn’t follow the Query Expression API
since it has =<rhs> on its construction: lookups are always the end of
a lookup expression.

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 lhs. The connection
can be used for compiling vendor specific SQL. If lhs is not
None, use it as the processed lhs instead of self.lhs.

This document is for Django's development version, which can be significantly different from previous releases. For older releases, use the version selector floating in the bottom right corner of this page.