custom-lookups.txt 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. ==============
  2. Custom Lookups
  3. ==============
  4. .. versionadded:: 1.7
  5. .. currentmodule:: django.db.models
  6. Django offers a wide variety of :ref:`built-in lookups <field-lookups>` for
  7. filtering (for example, ``exact`` and ``icontains``). This documentation
  8. explains how to write custom lookups and how to alter the working of existing
  9. lookups. For the API references of lookups, see the :doc:`/ref/models/lookups`.
  10. A simple lookup example
  11. ~~~~~~~~~~~~~~~~~~~~~~~
  12. Let's start with a simple custom lookup. We will write a custom lookup ``ne``
  13. which works opposite to ``exact``. ``Author.objects.filter(name__ne='Jack')``
  14. will translate to the SQL::
  15. "author"."name" <> 'Jack'
  16. This SQL is backend independent, so we don't need to worry about different
  17. databases.
  18. There are two steps to making this work. Firstly we need to implement the
  19. lookup, then we need to tell Django about it. The implementation is quite
  20. straightforward::
  21. from django.db.models import Lookup
  22. class NotEqual(Lookup):
  23. lookup_name = 'ne'
  24. def as_sql(self, qn, connection):
  25. lhs, lhs_params = self.process_lhs(qn, connection)
  26. rhs, rhs_params = self.process_rhs(qn, connection)
  27. params = lhs_params + rhs_params
  28. return '%s <> %s' % (lhs, rhs), params
  29. To register the ``NotEqual`` lookup we will just need to call
  30. ``register_lookup`` on the field class we want the lookup to be available. In
  31. this case, the lookup makes sense on all ``Field`` subclasses, so we register
  32. it with ``Field`` directly::
  33. from django.db.models.fields import Field
  34. Field.register_lookup(NotEqual)
  35. Lookup registration can also be done using a decorator pattern::
  36. from django.db.models.fields import Field
  37. @Field.registerLookup
  38. class NotEqualLookup(Lookup):
  39. # ...
  40. .. versionchanged:: 1.8
  41. The ability to use the decorator pattern was added.
  42. We can now use ``foo__ne`` for any field ``foo``. You will need to ensure that
  43. this registration happens before you try to create any querysets using it. You
  44. could place the implementation in a ``models.py`` file, or register the lookup
  45. in the ``ready()`` method of an ``AppConfig``.
  46. Taking a closer look at the implementation, the first required attribute is
  47. ``lookup_name``. This allows the ORM to understand how to interpret ``name__ne``
  48. and use ``NotEqual`` to generate the SQL. By convention, these names are always
  49. lowercase strings containing only letters, but the only hard requirement is
  50. that it must not contain the string ``__``.
  51. We then need to define the ``as_sql`` method. This takes a ``SQLCompiler``
  52. object, called ``qn``, and the active database connection. ``SQLCompiler``
  53. objects are not documented, but the only thing we need to know about them is
  54. that they have a ``compile()`` method which returns a tuple containing a SQL
  55. string, and the parameters to be interpolated into that string. In most cases,
  56. you don't need to use it directly and can pass it on to ``process_lhs()`` and
  57. ``process_rhs()``.
  58. A ``Lookup`` works against two values, ``lhs`` and ``rhs``, standing for
  59. left-hand side and right-hand side. The left-hand side is usually a field
  60. reference, but it can be anything implementing the :ref:`query expression API
  61. <query-expression>`. The right-hand is the value given by the user. In the
  62. example ``Author.objects.filter(name__ne='Jack')``, the left-hand side is a
  63. reference to the ``name`` field of the ``Author`` model, and ``'Jack'`` is the
  64. right-hand side.
  65. We call ``process_lhs`` and ``process_rhs`` to convert them into the values we
  66. need for SQL using the ``qn`` object described before. These methods return
  67. tuples containing some SQL and the parameters to be interpolated into that SQL,
  68. just as we need to return from our ``as_sql`` method. In the above example,
  69. ``process_lhs`` returns ``('"author"."name"', [])`` and ``process_rhs`` returns
  70. ``('"%s"', ['Jack'])``. In this example there were no parameters for the left
  71. hand side, but this would depend on the object we have, so we still need to
  72. include them in the parameters we return.
  73. Finally we combine the parts into a SQL expression with ``<>``, and supply all
  74. the parameters for the query. We then return a tuple containing the generated
  75. SQL string and the parameters.
  76. A simple transformer example
  77. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  78. The custom lookup above is great, but in some cases you may want to be able to
  79. chain lookups together. For example, let's suppose we are building an
  80. application where we want to make use of the ``abs()`` operator.
  81. We have an ``Experiment`` model which records a start value, end value, and the
  82. change (start - end). We would like to find all experiments where the change
  83. was equal to a certain amount (``Experiment.objects.filter(change__abs=27)``),
  84. or where it did not exceed a certain amount
  85. (``Experiment.objects.filter(change__abs__lt=27)``).
  86. .. note::
  87. This example is somewhat contrived, but it nicely demonstrates the range of
  88. functionality which is possible in a database backend independent manner,
  89. and without duplicating functionality already in Django.
  90. We will start by writing a ``AbsoluteValue`` transformer. This will use the SQL
  91. function ``ABS()`` to transform the value before comparison::
  92. from django.db.models import Transform
  93. class AbsoluteValue(Transform):
  94. lookup_name = 'abs'
  95. def as_sql(self, qn, connection):
  96. lhs, params = qn.compile(self.lhs)
  97. return "ABS(%s)" % lhs, params
  98. Next, let's register it for ``IntegerField``::
  99. from django.db.models import IntegerField
  100. IntegerField.register_lookup(AbsoluteValue)
  101. We can now run the queries we had before.
  102. ``Experiment.objects.filter(change__abs=27)`` will generate the following SQL::
  103. SELECT ... WHERE ABS("experiments"."change") = 27
  104. By using ``Transform`` instead of ``Lookup`` it means we are able to chain
  105. further lookups afterwards. So
  106. ``Experiment.objects.filter(change__abs__lt=27)`` will generate the following
  107. SQL::
  108. SELECT ... WHERE ABS("experiments"."change") < 27
  109. Note that in case there is no other lookup specified, Django interprets
  110. ``change__abs=27`` as ``change__abs__exact=27``.
  111. When looking for which lookups are allowable after the ``Transform`` has been
  112. applied, Django uses the ``output_field`` attribute. We didn't need to specify
  113. this here as it didn't change, but supposing we were applying ``AbsoluteValue``
  114. to some field which represents a more complex type (for example a point
  115. relative to an origin, or a complex number) then we may have wanted to specify
  116. ``output_field = FloatField``, which will ensure that further lookups like
  117. ``abs__lte`` behave as they would for a ``FloatField``.
  118. Writing an efficient abs__lt lookup
  119. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  120. When using the above written ``abs`` lookup, the SQL produced will not use
  121. indexes efficiently in some cases. In particular, when we use
  122. ``change__abs__lt=27``, this is equivalent to ``change__gt=-27`` AND
  123. ``change__lt=27``. (For the ``lte`` case we could use the SQL ``BETWEEN``).
  124. So we would like ``Experiment.objects.filter(change__abs__lt=27)`` to generate
  125. the following SQL::
  126. SELECT .. WHERE "experiments"."change" < 27 AND "experiments"."change" > -27
  127. The implementation is::
  128. from django.db.models import Lookup
  129. class AbsoluteValueLessThan(Lookup):
  130. lookup_name = 'lt'
  131. def as_sql(self, qn, connection):
  132. lhs, lhs_params = qn.compile(self.lhs.lhs)
  133. rhs, rhs_params = self.process_rhs(qn, connection)
  134. params = lhs_params + rhs_params + lhs_params + rhs_params
  135. return '%s < %s AND %s > -%s' % (lhs, rhs, lhs, rhs), params
  136. AbsoluteValue.register_lookup(AbsoluteValueLessThan)
  137. There are a couple of notable things going on. First, ``AbsoluteValueLessThan``
  138. isn't calling ``process_lhs()``. Instead it skips the transformation of the
  139. ``lhs`` done by ``AbsoluteValue`` and uses the original ``lhs``. That is, we
  140. want to get ``27`` not ``ABS(27)``. Referring directly to ``self.lhs.lhs`` is
  141. safe as ``AbsoluteValueLessThan`` can be accessed only from the
  142. ``AbsoluteValue`` lookup, that is the ``lhs`` is always an instance of
  143. ``AbsoluteValue``.
  144. Notice also that as both sides are used multiple times in the query the params
  145. need to contain ``lhs_params`` and ``rhs_params`` multiple times.
  146. The final query does the inversion (``27`` to ``-27``) directly in the
  147. database. The reason for doing this is that if the ``self.rhs`` is something else
  148. than a plain integer value (for example an ``F()`` reference) we can't do the
  149. transformations in Python.
  150. .. note::
  151. In fact, most lookups with ``__abs`` could be implemented as range queries
  152. like this, and on most database backends it is likely to be more sensible to
  153. do so as you can make use of the indexes. However with PostgreSQL you may
  154. want to add an index on ``abs(change)`` which would allow these queries to
  155. be very efficient.
  156. A bilateral transformer example
  157. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  158. The ``AbsoluteValue`` example we discussed previously is a transformation which
  159. applies to the left-hand side of the lookup. There may be some cases where you
  160. want the transformation to be applied to both the left-hand side and the
  161. right-hand side. For instance, if you want to filter a queryset based on the
  162. equality of the left and right-hand side insensitively to some SQL function.
  163. Let's examine the simple example of case-insensitive transformation here. This
  164. transformation isn't very useful in practice as Django already comes with a bunch
  165. of built-in case-insensitive lookups, but it will be a nice demonstration of
  166. bilateral transformations in a database-agnostic way.
  167. We define an ``UpperCase`` transformer which uses the SQL function ``UPPER()`` to
  168. transform the values before comparison. We define
  169. :attr:`bilateral = True <django.db.models.Transform.bilateral>` to indicate that
  170. this transformation should apply to both ``lhs`` and ``rhs``::
  171. from django.db.models import Transform
  172. class UpperCase(Transform):
  173. lookup_name = 'upper'
  174. bilateral = True
  175. def as_sql(self, qn, connection):
  176. lhs, params = qn.compile(self.lhs)
  177. return "UPPER(%s)" % lhs, params
  178. Next, let's register it::
  179. from django.db.models import CharField, TextField
  180. CharField.register_lookup(UpperCase)
  181. TextField.register_lookup(UpperCase)
  182. Now, the queryset ``Author.objects.filter(name__upper="doe")`` will generate a case
  183. insensitive query like this::
  184. SELECT ... WHERE UPPER("author"."name") = UPPER('doe')
  185. Writing alternative implementations for existing lookups
  186. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  187. Sometimes different database vendors require different SQL for the same
  188. operation. For this example we will rewrite a custom implementation for
  189. MySQL for the NotEqual operator. Instead of ``<>`` we will be using ``!=``
  190. operator. (Note that in reality almost all databases support both, including
  191. all the official databases supported by Django).
  192. We can change the behavior on a specific backend by creating a subclass of
  193. ``NotEqual`` with a ``as_mysql`` method::
  194. class MySQLNotEqual(NotEqual):
  195. def as_mysql(self, qn, connection):
  196. lhs, lhs_params = self.process_lhs(qn, connection)
  197. rhs, rhs_params = self.process_rhs(qn, connection)
  198. params = lhs_params + rhs_params
  199. return '%s != %s' % (lhs, rhs), params
  200. Field.register_lookup(MySQLNotExact)
  201. We can then register it with ``Field``. It takes the place of the original
  202. ``NotEqual`` class as it has the same ``lookup_name``.
  203. When compiling a query, Django first looks for ``as_%s % connection.vendor``
  204. methods, and then falls back to ``as_sql``. The vendor names for the in-built
  205. backends are ``sqlite``, ``postgresql``, ``oracle`` and ``mysql``.
  206. How Django determines the lookups and transforms which are used
  207. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  208. In some cases you may wish to dynamically change which ``Transform`` or
  209. ``Lookup`` is returned based on the name passed in, rather than fixing it. As
  210. an example, you could have a field which stores coordinates or an arbitrary
  211. dimension, and wish to allow a syntax like ``.filter(coords__x7=4)`` to return
  212. the objects where the 7th coordinate has value 4. In order to do this, you
  213. would override ``get_lookup`` with something like::
  214. class CoordinatesField(Field):
  215. def get_lookup(self, lookup_name):
  216. if lookup_name.startswith('x'):
  217. try:
  218. dimension = int(lookup_name[1:])
  219. except ValueError:
  220. pass
  221. finally:
  222. return get_coordinate_lookup(dimension)
  223. return super(CoordinatesField, self).get_lookup(lookup_name)
  224. You would then define ``get_coordinate_lookup`` appropriately to return a
  225. ``Lookup`` subclass which handles the relevant value of ``dimension``.
  226. There is a similarly named method called ``get_transform()``. ``get_lookup()``
  227. should always return a ``Lookup`` subclass, and ``get_transform()`` a
  228. ``Transform`` subclass. It is important to remember that ``Transform``
  229. objects can be further filtered on, and ``Lookup`` objects cannot.
  230. When filtering, if there is only one lookup name remaining to be resolved, we
  231. will look for a ``Lookup``. If there are multiple names, it will look for a
  232. ``Transform``. In the situation where there is only one name and a ``Lookup``
  233. is not found, we look for a ``Transform`` and then the ``exact`` lookup on that
  234. ``Transform``. All call sequences always end with a ``Lookup``. To clarify:
  235. - ``.filter(myfield__mylookup)`` will call ``myfield.get_lookup('mylookup')``.
  236. - ``.filter(myfield__mytransform__mylookup)`` will call
  237. ``myfield.get_transform('mytransform')``, and then
  238. ``mytransform.get_lookup('mylookup')``.
  239. - ``.filter(myfield__mytransform)`` will first call
  240. ``myfield.get_lookup('mytransform')``, which will fail, so it will fall back
  241. to calling ``myfield.get_transform('mytransform')`` and then
  242. ``mytransform.get_lookup('exact')``.