Lookup API reference¶
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.
The lookup API has two components: a RegisterLookupMixin class
that registers lookups, and the Query Expression API, a
set of methods that a class has to implement to be registrable as a lookup.
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:
- Fields part (e.g. Book.objects.filter(author__best_friends__first_name...);
- Transforms part (may be omitted) (e.g. __lower__first3chars__reversed);
- A lookup (e.g. __icontains) that, if omitted, defaults to__exact.
Registration API¶
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.
- 
class lookups.RegisterLookupMixin¶
- A mixin that implements the lookup API on a class. - 
classmethod register_lookup(lookup, lookup_name=None)¶
- Registers a new lookup in the class. For example - DateField.register_lookup(YearExact)will register- YearExactlookup on- DateField. It overrides a lookup that already exists with the same name.- lookup_namewill be used for this lookup if provided, otherwise- lookup.lookup_namewill be used.Changed in Django 1.9:- The - lookup_nameparameter was added.
 
- 
classmethod 
For a class to be a lookup, it must follow the Query Expression API. Lookup and Transform naturally
follow this API.
The Query Expression API¶
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:
- 
as_sql(self, compiler, connection)¶
- Responsible for producing the query string and parameters for the expression. The - compileris an- SQLCompilerobject, which has a- compile()method that can be used to compile other expressions. The- connectionis 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.- 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_vendorname(self, compiler, connection)¶
- Works like - as_sql()method. When an expression is compiled by- compiler.compile(), Django will first try to call- as_vendorname(), where- vendornameis the vendor name of the backend used for executing the query. The- vendornameis one of- postgresql,- oracle,- sqlite, or- mysqlfor Django’s built-in backends.
- 
get_lookup(lookup_name)¶
- Must return the lookup named - lookup_name. For instance, by returning- self.output_field.get_lookup(lookup_name).
- 
get_transform(transform_name)¶
- Must return the lookup named - transform_name. For instance, by returning- self.output_field.get_transform(transform_name).
Transform reference¶
- 
class Transform[source]¶
- A - Transformis a generic class to implement field transformations. A prominent example is- __yearthat transforms a- DateFieldinto a- IntegerField.- The notation to use a - Transformin an lookup expression is- <expression>__<transformation>(e.g.- date__year).- This class follows the Query Expression API, which implies that you can use - <expression>__<transform1>__<transform2>. 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.Changed in Django 1.9:- Transformis now a subclass of- Func.- 
bilateral¶
- A boolean indicating whether this transformation should apply to both - lhsand- rhs. Bilateral transformations will be applied to- rhsin the same order as they appear in the lookup expression. By default it is set to- False. For example usage, see Custom Lookups.
 - 
lhs¶
- The left-hand side - what is being transformed. It must follow the Query Expression API. 
 - 
lookup_name¶
- The name of the lookup, used for identifying it on parsing query expressions. It cannot contain the string - "__".
 
- 
Lookup reference¶
- 
class Lookup[source]¶
- A - Lookupis 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_namethat is used to produce a boolean comparison between- lhsand- rhssuch as- lhs in rhsor- 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.- 
lhs¶
- The left-hand side - what is being looked up. The object must follow the Query Expression API. 
 - 
rhs¶
- The right-hand side - what - lhsis being compared against. It can be a plain value, or something that compiles into SQL, typically an- F()object or a- QuerySet.
 - 
lookup_name¶
- The name of this lookup, used to identify it on parsing query expressions. It cannot contain the string - "__".
 - 
process_lhs(compiler, connection, lhs=None)[source]¶
- Returns a tuple - (lhs_string, lhs_params), as returned by- compiler.compile(lhs). This method can be overridden to tune how the- lhsis processed.- compileris an- SQLCompilerobject, to be used like- compiler.compile(lhs)for compiling- lhs. The- connectioncan be used for compiling vendor specific SQL. If- lhsis not- None, use it as the processed- lhsinstead of- self.lhs.
 - 
process_rhs(compiler, connection)[source]¶
- Behaves the same way as - process_lhs(), for the right-hand side.
 
- 
 
          