expressions.txt 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392
  1. =================
  2. Query Expressions
  3. =================
  4. .. currentmodule:: django.db.models
  5. Query expressions describe a value or a computation that can be used as part of
  6. an update, create, filter, order by, annotation, or aggregate. When an
  7. expression outputs a boolean value, it may be used directly in filters. There
  8. are a number of built-in expressions (documented below) that can be used to
  9. help you write queries. Expressions can be combined, or in some cases nested,
  10. to form more complex computations.
  11. Supported arithmetic
  12. ====================
  13. Django supports negation, addition, subtraction, multiplication, division,
  14. modulo arithmetic, and the power operator on query expressions, using Python
  15. constants, variables, and even other expressions.
  16. .. _output-field:
  17. Output field
  18. ============
  19. Many of the expressions documented in this section support an optional
  20. ``output_field`` parameter. If given, Django will load the value into that
  21. field after retrieving it from the database.
  22. ``output_field`` takes a model field instance, like ``IntegerField()`` or
  23. ``BooleanField()``. Usually, the field doesn't need any arguments, like
  24. ``max_length``, since field arguments relate to data validation which will not
  25. be performed on the expression's output value.
  26. ``output_field`` is only required when Django is unable to automatically
  27. determine the result's field type, such as complex expressions that mix field
  28. types. For example, adding a ``DecimalField()`` and a ``FloatField()`` requires
  29. an output field, like ``output_field=FloatField()``.
  30. Some examples
  31. =============
  32. .. code-block:: pycon
  33. >>> from django.db.models import Count, F, Value
  34. >>> from django.db.models.functions import Length, Upper
  35. >>> from django.db.models.lookups import GreaterThan
  36. # Find companies that have more employees than chairs.
  37. >>> Company.objects.filter(num_employees__gt=F("num_chairs"))
  38. # Find companies that have at least twice as many employees
  39. # as chairs. Both the querysets below are equivalent.
  40. >>> Company.objects.filter(num_employees__gt=F("num_chairs") * 2)
  41. >>> Company.objects.filter(num_employees__gt=F("num_chairs") + F("num_chairs"))
  42. # How many chairs are needed for each company to seat all employees?
  43. >>> company = (
  44. ... Company.objects.filter(num_employees__gt=F("num_chairs"))
  45. ... .annotate(chairs_needed=F("num_employees") - F("num_chairs"))
  46. ... .first()
  47. ... )
  48. >>> company.num_employees
  49. 120
  50. >>> company.num_chairs
  51. 50
  52. >>> company.chairs_needed
  53. 70
  54. # Create a new company using expressions.
  55. >>> company = Company.objects.create(name="Google", ticker=Upper(Value("goog")))
  56. # Be sure to refresh it if you need to access the field.
  57. >>> company.refresh_from_db()
  58. >>> company.ticker
  59. 'GOOG'
  60. # Annotate models with an aggregated value. Both forms
  61. # below are equivalent.
  62. >>> Company.objects.annotate(num_products=Count("products"))
  63. >>> Company.objects.annotate(num_products=Count(F("products")))
  64. # Aggregates can contain complex computations also
  65. >>> Company.objects.annotate(num_offerings=Count(F("products") + F("services")))
  66. # Expressions can also be used in order_by(), either directly
  67. >>> Company.objects.order_by(Length("name").asc())
  68. >>> Company.objects.order_by(Length("name").desc())
  69. # or using the double underscore lookup syntax.
  70. >>> from django.db.models import CharField
  71. >>> from django.db.models.functions import Length
  72. >>> CharField.register_lookup(Length)
  73. >>> Company.objects.order_by("name__length")
  74. # Boolean expression can be used directly in filters.
  75. >>> from django.db.models import Exists, OuterRef
  76. >>> Company.objects.filter(
  77. ... Exists(Employee.objects.filter(company=OuterRef("pk"), salary__gt=10))
  78. ... )
  79. # Lookup expressions can also be used directly in filters
  80. >>> Company.objects.filter(GreaterThan(F("num_employees"), F("num_chairs")))
  81. # or annotations.
  82. >>> Company.objects.annotate(
  83. ... need_chairs=GreaterThan(F("num_employees"), F("num_chairs")),
  84. ... )
  85. Built-in Expressions
  86. ====================
  87. .. note::
  88. These expressions are defined in ``django.db.models.expressions`` and
  89. ``django.db.models.aggregates``, but for convenience they're available and
  90. usually imported from :mod:`django.db.models`.
  91. ``F()`` expressions
  92. -------------------
  93. .. class:: F
  94. An ``F()`` object represents the value of a model field, transformed value of a
  95. model field, or annotated column. It makes it possible to refer to model field
  96. values and perform database operations using them without actually having to
  97. pull them out of the database into Python memory.
  98. Instead, Django uses the ``F()`` object to generate an SQL expression that
  99. describes the required operation at the database level.
  100. Let's try this with an example. Normally, one might do something like this::
  101. # Tintin filed a news story!
  102. reporter = Reporters.objects.get(name="Tintin")
  103. reporter.stories_filed += 1
  104. reporter.save()
  105. Here, we have pulled the value of ``reporter.stories_filed`` from the database
  106. into memory and manipulated it using familiar Python operators, and then saved
  107. the object back to the database. But instead we could also have done::
  108. from django.db.models import F
  109. reporter = Reporters.objects.get(name="Tintin")
  110. reporter.stories_filed = F("stories_filed") + 1
  111. reporter.save()
  112. Although ``reporter.stories_filed = F('stories_filed') + 1`` looks like a
  113. normal Python assignment of value to an instance attribute, in fact it's an SQL
  114. construct describing an operation on the database.
  115. When Django encounters an instance of ``F()``, it overrides the standard Python
  116. operators to create an encapsulated SQL expression; in this case, one which
  117. instructs the database to increment the database field represented by
  118. ``reporter.stories_filed``.
  119. Whatever value is or was on ``reporter.stories_filed``, Python never gets to
  120. know about it - it is dealt with entirely by the database. All Python does,
  121. through Django's ``F()`` class, is create the SQL syntax to refer to the field
  122. and describe the operation.
  123. To access the new value saved this way, the object must be reloaded::
  124. reporter = Reporters.objects.get(pk=reporter.pk)
  125. # Or, more succinctly:
  126. reporter.refresh_from_db()
  127. As well as being used in operations on single instances as above, ``F()`` can
  128. be used on ``QuerySets`` of object instances, with ``update()``. This reduces
  129. the two queries we were using above - the ``get()`` and the
  130. :meth:`~Model.save()` - to just one::
  131. reporter = Reporters.objects.filter(name="Tintin")
  132. reporter.update(stories_filed=F("stories_filed") + 1)
  133. We can also use :meth:`~django.db.models.query.QuerySet.update()` to increment
  134. the field value on multiple objects - which could be very much faster than
  135. pulling them all into Python from the database, looping over them, incrementing
  136. the field value of each one, and saving each one back to the database::
  137. Reporter.objects.update(stories_filed=F("stories_filed") + 1)
  138. ``F()`` therefore can offer performance advantages by:
  139. * getting the database, rather than Python, to do work
  140. * reducing the number of queries some operations require
  141. .. _slicing-using-f:
  142. Slicing ``F()`` expressions
  143. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  144. .. versionadded:: 5.1
  145. For string-based fields, text-based fields, and
  146. :class:`~django.contrib.postgres.fields.ArrayField`, you can use Python's
  147. array-slicing syntax. The indices are 0-based and the ``step`` argument to
  148. ``slice`` is not supported. For example:
  149. .. code-block:: pycon
  150. >>> # Replacing a name with a substring of itself.
  151. >>> writer = Writers.objects.get(name="Priyansh")
  152. >>> writer.name = F("name")[1:5]
  153. >>> writer.save()
  154. >>> writer.refresh_from_db()
  155. >>> writer.name
  156. 'riya'
  157. .. _avoiding-race-conditions-using-f:
  158. Avoiding race conditions using ``F()``
  159. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  160. Another useful benefit of ``F()`` is that having the database - rather than
  161. Python - update a field's value avoids a *race condition*.
  162. If two Python threads execute the code in the first example above, one thread
  163. could retrieve, increment, and save a field's value after the other has
  164. retrieved it from the database. The value that the second thread saves will be
  165. based on the original value; the work of the first thread will be lost.
  166. If the database is responsible for updating the field, the process is more
  167. robust: it will only ever update the field based on the value of the field in
  168. the database when the :meth:`~Model.save()` or ``update()`` is executed, rather
  169. than based on its value when the instance was retrieved.
  170. ``F()`` assignments persist after ``Model.save()``
  171. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  172. ``F()`` objects assigned to model fields persist after saving the model
  173. instance and will be applied on each :meth:`~Model.save()`. For example::
  174. reporter = Reporters.objects.get(name="Tintin")
  175. reporter.stories_filed = F("stories_filed") + 1
  176. reporter.save()
  177. reporter.name = "Tintin Jr."
  178. reporter.save()
  179. ``stories_filed`` will be updated twice in this case. If it's initially ``1``,
  180. the final value will be ``3``. This persistence can be avoided by reloading the
  181. model object after saving it, for example, by using
  182. :meth:`~Model.refresh_from_db()`.
  183. Using ``F()`` in filters
  184. ~~~~~~~~~~~~~~~~~~~~~~~~
  185. ``F()`` is also very useful in ``QuerySet`` filters, where they make it
  186. possible to filter a set of objects against criteria based on their field
  187. values, rather than on Python values.
  188. This is documented in :ref:`using F() expressions in queries
  189. <using-f-expressions-in-filters>`.
  190. .. _using-f-with-annotations:
  191. Using ``F()`` with annotations
  192. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  193. ``F()`` can be used to create dynamic fields on your models by combining
  194. different fields with arithmetic::
  195. company = Company.objects.annotate(chairs_needed=F("num_employees") - F("num_chairs"))
  196. If the fields that you're combining are of different types you'll need to tell
  197. Django what kind of field will be returned. Most expressions support
  198. :ref:`output_field<output-field>` for this case, but since ``F()`` does not, you
  199. will need to wrap the expression with :class:`ExpressionWrapper`::
  200. from django.db.models import DateTimeField, ExpressionWrapper, F
  201. Ticket.objects.annotate(
  202. expires=ExpressionWrapper(
  203. F("active_at") + F("duration"), output_field=DateTimeField()
  204. )
  205. )
  206. When referencing relational fields such as ``ForeignKey``, ``F()`` returns the
  207. primary key value rather than a model instance:
  208. .. code-block:: pycon
  209. >>> car = Company.objects.annotate(built_by=F("manufacturer"))[0]
  210. >>> car.manufacturer
  211. <Manufacturer: Toyota>
  212. >>> car.built_by
  213. 3
  214. .. _using-f-to-sort-null-values:
  215. Using ``F()`` to sort null values
  216. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  217. Use ``F()`` and the ``nulls_first`` or ``nulls_last`` keyword argument to
  218. :meth:`.Expression.asc` or :meth:`~.Expression.desc` to control the ordering of
  219. a field's null values. By default, the ordering depends on your database.
  220. For example, to sort companies that haven't been contacted (``last_contacted``
  221. is null) after companies that have been contacted::
  222. from django.db.models import F
  223. Company.objects.order_by(F("last_contacted").desc(nulls_last=True))
  224. Using ``F()`` with logical operations
  225. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  226. ``F()`` expressions that output ``BooleanField`` can be logically negated with
  227. the inversion operator ``~F()``. For example, to swap the activation status of
  228. companies::
  229. from django.db.models import F
  230. Company.objects.update(is_active=~F("is_active"))
  231. .. _func-expressions:
  232. ``Func()`` expressions
  233. ----------------------
  234. ``Func()`` expressions are the base type of all expressions that involve
  235. database functions like ``COALESCE`` and ``LOWER``, or aggregates like ``SUM``.
  236. They can be used directly::
  237. from django.db.models import F, Func
  238. queryset.annotate(field_lower=Func(F("field"), function="LOWER"))
  239. or they can be used to build a library of database functions::
  240. class Lower(Func):
  241. function = "LOWER"
  242. queryset.annotate(field_lower=Lower("field"))
  243. But both cases will result in a queryset where each model is annotated with an
  244. extra attribute ``field_lower`` produced, roughly, from the following SQL:
  245. .. code-block:: sql
  246. SELECT
  247. ...
  248. LOWER("db_table"."field") as "field_lower"
  249. See :doc:`database-functions` for a list of built-in database functions.
  250. The ``Func`` API is as follows:
  251. .. class:: Func(*expressions, **extra)
  252. .. attribute:: function
  253. A class attribute describing the function that will be generated.
  254. Specifically, the ``function`` will be interpolated as the ``function``
  255. placeholder within :attr:`template`. Defaults to ``None``.
  256. .. attribute:: template
  257. A class attribute, as a format string, that describes the SQL that is
  258. generated for this function. Defaults to
  259. ``'%(function)s(%(expressions)s)'``.
  260. If you're constructing SQL like ``strftime('%W', 'date')`` and need a
  261. literal ``%`` character in the query, quadruple it (``%%%%``) in the
  262. ``template`` attribute because the string is interpolated twice: once
  263. during the template interpolation in ``as_sql()`` and once in the SQL
  264. interpolation with the query parameters in the database cursor.
  265. .. attribute:: arg_joiner
  266. A class attribute that denotes the character used to join the list of
  267. ``expressions`` together. Defaults to ``', '``.
  268. .. attribute:: arity
  269. A class attribute that denotes the number of arguments the function
  270. accepts. If this attribute is set and the function is called with a
  271. different number of expressions, ``TypeError`` will be raised. Defaults
  272. to ``None``.
  273. .. method:: as_sql(compiler, connection, function=None, template=None, arg_joiner=None, **extra_context)
  274. Generates the SQL fragment for the database function. Returns a tuple
  275. ``(sql, params)``, where ``sql`` is the SQL string, and ``params`` is
  276. the list or tuple of query parameters.
  277. The ``as_vendor()`` methods should use the ``function``, ``template``,
  278. ``arg_joiner``, and any other ``**extra_context`` parameters to
  279. customize the SQL as needed. For example:
  280. .. code-block:: python
  281. :caption: ``django/db/models/functions.py``
  282. class ConcatPair(Func):
  283. ...
  284. function = "CONCAT"
  285. ...
  286. def as_mysql(self, compiler, connection, **extra_context):
  287. return super().as_sql(
  288. compiler,
  289. connection,
  290. function="CONCAT_WS",
  291. template="%(function)s('', %(expressions)s)",
  292. **extra_context
  293. )
  294. To avoid an SQL injection vulnerability, ``extra_context`` :ref:`must
  295. not contain untrusted user input <avoiding-sql-injection-in-query-expressions>`
  296. as these values are interpolated into the SQL string rather than passed
  297. as query parameters, where the database driver would escape them.
  298. The ``*expressions`` argument is a list of positional expressions that the
  299. function will be applied to. The expressions will be converted to strings,
  300. joined together with ``arg_joiner``, and then interpolated into the ``template``
  301. as the ``expressions`` placeholder.
  302. Positional arguments can be expressions or Python values. Strings are
  303. assumed to be column references and will be wrapped in ``F()`` expressions
  304. while other values will be wrapped in ``Value()`` expressions.
  305. The ``**extra`` kwargs are ``key=value`` pairs that can be interpolated
  306. into the ``template`` attribute. To avoid an SQL injection vulnerability,
  307. ``extra`` :ref:`must not contain untrusted user input
  308. <avoiding-sql-injection-in-query-expressions>` as these values are interpolated
  309. into the SQL string rather than passed as query parameters, where the database
  310. driver would escape them.
  311. The ``function``, ``template``, and ``arg_joiner`` keywords can be used to
  312. replace the attributes of the same name without having to define your own
  313. class. :ref:`output_field<output-field>` can be used to define the expected
  314. return type.
  315. ``Aggregate()`` expressions
  316. ---------------------------
  317. An aggregate expression is a special case of a :ref:`Func() expression
  318. <func-expressions>` that informs the query that a ``GROUP BY`` clause
  319. is required. All of the :ref:`aggregate functions <aggregation-functions>`,
  320. like ``Sum()`` and ``Count()``, inherit from ``Aggregate()``.
  321. Since ``Aggregate``\s are expressions and wrap expressions, you can represent
  322. some complex computations::
  323. from django.db.models import Count
  324. Company.objects.annotate(
  325. managers_required=(Count("num_employees") / 4) + Count("num_managers")
  326. )
  327. The ``Aggregate`` API is as follows:
  328. .. class:: Aggregate(*expressions, output_field=None, distinct=False, filter=None, default=None, **extra)
  329. .. attribute:: template
  330. A class attribute, as a format string, that describes the SQL that is
  331. generated for this aggregate. Defaults to
  332. ``'%(function)s(%(distinct)s%(expressions)s)'``.
  333. .. attribute:: function
  334. A class attribute describing the aggregate function that will be
  335. generated. Specifically, the ``function`` will be interpolated as the
  336. ``function`` placeholder within :attr:`template`. Defaults to ``None``.
  337. .. attribute:: window_compatible
  338. Defaults to ``True`` since most aggregate functions can be used as the
  339. source expression in :class:`~django.db.models.expressions.Window`.
  340. .. attribute:: allow_distinct
  341. A class attribute determining whether or not this aggregate function
  342. allows passing a ``distinct`` keyword argument. If set to ``False``
  343. (default), ``TypeError`` is raised if ``distinct=True`` is passed.
  344. .. attribute:: empty_result_set_value
  345. Defaults to ``None`` since most aggregate functions result in ``NULL``
  346. when applied to an empty result set.
  347. The ``expressions`` positional arguments can include expressions, transforms of
  348. the model field, or the names of model fields. They will be converted to a
  349. string and used as the ``expressions`` placeholder within the ``template``.
  350. The ``distinct`` argument determines whether or not the aggregate function
  351. should be invoked for each distinct value of ``expressions`` (or set of
  352. values, for multiple ``expressions``). The argument is only supported on
  353. aggregates that have :attr:`~Aggregate.allow_distinct` set to ``True``.
  354. The ``filter`` argument takes a :class:`Q object <django.db.models.Q>` that's
  355. used to filter the rows that are aggregated. See :ref:`conditional-aggregation`
  356. and :ref:`filtering-on-annotations` for example usage.
  357. The ``default`` argument takes a value that will be passed along with the
  358. aggregate to :class:`~django.db.models.functions.Coalesce`. This is useful for
  359. specifying a value to be returned other than ``None`` when the queryset (or
  360. grouping) contains no entries.
  361. The ``**extra`` kwargs are ``key=value`` pairs that can be interpolated
  362. into the ``template`` attribute.
  363. Creating your own Aggregate Functions
  364. -------------------------------------
  365. You can create your own aggregate functions, too. At a minimum, you need to
  366. define ``function``, but you can also completely customize the SQL that is
  367. generated. Here's a brief example::
  368. from django.db.models import Aggregate
  369. class Sum(Aggregate):
  370. # Supports SUM(ALL field).
  371. function = "SUM"
  372. template = "%(function)s(%(all_values)s%(expressions)s)"
  373. allow_distinct = False
  374. arity = 1
  375. def __init__(self, expression, all_values=False, **extra):
  376. super().__init__(expression, all_values="ALL " if all_values else "", **extra)
  377. ``Value()`` expressions
  378. -----------------------
  379. .. class:: Value(value, output_field=None)
  380. A ``Value()`` object represents the smallest possible component of an
  381. expression: a simple value. When you need to represent the value of an integer,
  382. boolean, or string within an expression, you can wrap that value within a
  383. ``Value()``.
  384. You will rarely need to use ``Value()`` directly. When you write the expression
  385. ``F('field') + 1``, Django implicitly wraps the ``1`` in a ``Value()``,
  386. allowing simple values to be used in more complex expressions. You will need to
  387. use ``Value()`` when you want to pass a string to an expression. Most
  388. expressions interpret a string argument as the name of a field, like
  389. ``Lower('name')``.
  390. The ``value`` argument describes the value to be included in the expression,
  391. such as ``1``, ``True``, or ``None``. Django knows how to convert these Python
  392. values into their corresponding database type.
  393. If no :ref:`output_field<output-field>` is specified, it will be inferred from
  394. the type of the provided ``value`` for many common types. For example, passing
  395. an instance of :py:class:`datetime.datetime` as ``value`` defaults
  396. ``output_field`` to :class:`~django.db.models.DateTimeField`.
  397. ``ExpressionWrapper()`` expressions
  398. -----------------------------------
  399. .. class:: ExpressionWrapper(expression, output_field)
  400. ``ExpressionWrapper`` surrounds another expression and provides access to
  401. properties, such as :ref:`output_field<output-field>`, that may not be
  402. available on other expressions. ``ExpressionWrapper`` is necessary when using
  403. arithmetic on ``F()`` expressions with different types as described in
  404. :ref:`using-f-with-annotations`.
  405. Conditional expressions
  406. -----------------------
  407. Conditional expressions allow you to use :keyword:`if` ... :keyword:`elif` ...
  408. :keyword:`else` logic in queries. Django natively supports SQL ``CASE``
  409. expressions. For more details see :doc:`conditional-expressions`.
  410. ``Subquery()`` expressions
  411. --------------------------
  412. .. class:: Subquery(queryset, output_field=None)
  413. You can add an explicit subquery to a ``QuerySet`` using the ``Subquery``
  414. expression.
  415. For example, to annotate each post with the email address of the author of the
  416. newest comment on that post:
  417. .. code-block:: pycon
  418. >>> from django.db.models import OuterRef, Subquery
  419. >>> newest = Comment.objects.filter(post=OuterRef("pk")).order_by("-created_at")
  420. >>> Post.objects.annotate(newest_commenter_email=Subquery(newest.values("email")[:1]))
  421. On PostgreSQL, the SQL looks like:
  422. .. code-block:: sql
  423. SELECT "post"."id", (
  424. SELECT U0."email"
  425. FROM "comment" U0
  426. WHERE U0."post_id" = ("post"."id")
  427. ORDER BY U0."created_at" DESC LIMIT 1
  428. ) AS "newest_commenter_email" FROM "post"
  429. .. note::
  430. The examples in this section are designed to show how to force
  431. Django to execute a subquery. In some cases it may be possible to
  432. write an equivalent queryset that performs the same task more
  433. clearly or efficiently.
  434. Referencing columns from the outer queryset
  435. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  436. .. class:: OuterRef(field)
  437. Use ``OuterRef`` when a queryset in a ``Subquery`` needs to refer to a field
  438. from the outer query or its transform. It acts like an :class:`F` expression
  439. except that the check to see if it refers to a valid field isn't made until the
  440. outer queryset is resolved.
  441. Instances of ``OuterRef`` may be used in conjunction with nested instances
  442. of ``Subquery`` to refer to a containing queryset that isn't the immediate
  443. parent. For example, this queryset would need to be within a nested pair of
  444. ``Subquery`` instances to resolve correctly:
  445. .. code-block:: pycon
  446. >>> Book.objects.filter(author=OuterRef(OuterRef("pk")))
  447. Limiting a subquery to a single column
  448. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  449. There are times when a single column must be returned from a ``Subquery``, for
  450. instance, to use a ``Subquery`` as the target of an ``__in`` lookup. To return
  451. all comments for posts published within the last day:
  452. .. code-block:: pycon
  453. >>> from datetime import timedelta
  454. >>> from django.utils import timezone
  455. >>> one_day_ago = timezone.now() - timedelta(days=1)
  456. >>> posts = Post.objects.filter(published_at__gte=one_day_ago)
  457. >>> Comment.objects.filter(post__in=Subquery(posts.values("pk")))
  458. In this case, the subquery must use :meth:`~.QuerySet.values`
  459. to return only a single column: the primary key of the post.
  460. Limiting the subquery to a single row
  461. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  462. To prevent a subquery from returning multiple rows, a slice (``[:1]``) of the
  463. queryset is used:
  464. .. code-block:: pycon
  465. >>> subquery = Subquery(newest.values("email")[:1])
  466. >>> Post.objects.annotate(newest_commenter_email=subquery)
  467. In this case, the subquery must only return a single column *and* a single
  468. row: the email address of the most recently created comment.
  469. (Using :meth:`~.QuerySet.get` instead of a slice would fail because the
  470. ``OuterRef`` cannot be resolved until the queryset is used within a
  471. ``Subquery``.)
  472. ``Exists()`` subqueries
  473. ~~~~~~~~~~~~~~~~~~~~~~~
  474. .. class:: Exists(queryset)
  475. ``Exists`` is a ``Subquery`` subclass that uses an SQL ``EXISTS`` statement. In
  476. many cases it will perform better than a subquery since the database is able to
  477. stop evaluation of the subquery when a first matching row is found.
  478. For example, to annotate each post with whether or not it has a comment from
  479. within the last day:
  480. .. code-block:: pycon
  481. >>> from django.db.models import Exists, OuterRef
  482. >>> from datetime import timedelta
  483. >>> from django.utils import timezone
  484. >>> one_day_ago = timezone.now() - timedelta(days=1)
  485. >>> recent_comments = Comment.objects.filter(
  486. ... post=OuterRef("pk"),
  487. ... created_at__gte=one_day_ago,
  488. ... )
  489. >>> Post.objects.annotate(recent_comment=Exists(recent_comments))
  490. On PostgreSQL, the SQL looks like:
  491. .. code-block:: sql
  492. SELECT "post"."id", "post"."published_at", EXISTS(
  493. SELECT (1) as "a"
  494. FROM "comment" U0
  495. WHERE (
  496. U0."created_at" >= YYYY-MM-DD HH:MM:SS AND
  497. U0."post_id" = "post"."id"
  498. )
  499. LIMIT 1
  500. ) AS "recent_comment" FROM "post"
  501. It's unnecessary to force ``Exists`` to refer to a single column, since the
  502. columns are discarded and a boolean result is returned. Similarly, since
  503. ordering is unimportant within an SQL ``EXISTS`` subquery and would only
  504. degrade performance, it's automatically removed.
  505. You can query using ``NOT EXISTS`` with ``~Exists()``.
  506. Filtering on a ``Subquery()`` or ``Exists()`` expressions
  507. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  508. ``Subquery()`` that returns a boolean value and ``Exists()`` may be used as a
  509. ``condition`` in :class:`~django.db.models.expressions.When` expressions, or to
  510. directly filter a queryset:
  511. .. code-block:: pycon
  512. >>> recent_comments = Comment.objects.filter(...) # From above
  513. >>> Post.objects.filter(Exists(recent_comments))
  514. This will ensure that the subquery will not be added to the ``SELECT`` columns,
  515. which may result in a better performance.
  516. Using aggregates within a ``Subquery`` expression
  517. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  518. Aggregates may be used within a ``Subquery``, but they require a specific
  519. combination of :meth:`~.QuerySet.filter`, :meth:`~.QuerySet.values`, and
  520. :meth:`~.QuerySet.annotate` to get the subquery grouping correct.
  521. Assuming both models have a ``length`` field, to find posts where the post
  522. length is greater than the total length of all combined comments:
  523. .. code-block:: pycon
  524. >>> from django.db.models import OuterRef, Subquery, Sum
  525. >>> comments = Comment.objects.filter(post=OuterRef("pk")).order_by().values("post")
  526. >>> total_comments = comments.annotate(total=Sum("length")).values("total")
  527. >>> Post.objects.filter(length__gt=Subquery(total_comments))
  528. The initial ``filter(...)`` limits the subquery to the relevant parameters.
  529. ``order_by()`` removes the default :attr:`~django.db.models.Options.ordering`
  530. (if any) on the ``Comment`` model. ``values('post')`` aggregates comments by
  531. ``Post``. Finally, ``annotate(...)`` performs the aggregation. The order in
  532. which these queryset methods are applied is important. In this case, since the
  533. subquery must be limited to a single column, ``values('total')`` is required.
  534. This is the only way to perform an aggregation within a ``Subquery``, as
  535. using :meth:`~.QuerySet.aggregate` attempts to evaluate the queryset (and if
  536. there is an ``OuterRef``, this will not be possible to resolve).
  537. Raw SQL expressions
  538. -------------------
  539. .. currentmodule:: django.db.models.expressions
  540. .. class:: RawSQL(sql, params, output_field=None)
  541. Sometimes database expressions can't easily express a complex ``WHERE`` clause.
  542. In these edge cases, use the ``RawSQL`` expression. For example:
  543. .. code-block:: pycon
  544. >>> from django.db.models.expressions import RawSQL
  545. >>> queryset.annotate(val=RawSQL("select col from sometable where othercol = %s", (param,)))
  546. These extra lookups may not be portable to different database engines (because
  547. you're explicitly writing SQL code) and violate the DRY principle, so you
  548. should avoid them if possible.
  549. ``RawSQL`` expressions can also be used as the target of ``__in`` filters:
  550. .. code-block:: pycon
  551. >>> queryset.filter(id__in=RawSQL("select id from sometable where col = %s", (param,)))
  552. .. warning::
  553. To protect against `SQL injection attacks
  554. <https://en.wikipedia.org/wiki/SQL_injection>`_, you must escape any
  555. parameters that the user can control by using ``params``. ``params`` is a
  556. required argument to force you to acknowledge that you're not interpolating
  557. your SQL with user-provided data.
  558. You also must not quote placeholders in the SQL string. This example is
  559. vulnerable to SQL injection because of the quotes around ``%s``::
  560. RawSQL("select col from sometable where othercol = '%s'") # unsafe!
  561. You can read more about how Django's :ref:`SQL injection protection
  562. <sql-injection-protection>` works.
  563. Window functions
  564. ----------------
  565. Window functions provide a way to apply functions on partitions. Unlike a
  566. normal aggregation function which computes a final result for each set defined
  567. by the group by, window functions operate on :ref:`frames <window-frames>` and
  568. partitions, and compute the result for each row.
  569. You can specify multiple windows in the same query which in Django ORM would be
  570. equivalent to including multiple expressions in a :doc:`QuerySet.annotate()
  571. </topics/db/aggregation>` call. The ORM doesn't make use of named windows,
  572. instead they are part of the selected columns.
  573. .. class:: Window(expression, partition_by=None, order_by=None, frame=None, output_field=None)
  574. .. attribute:: template
  575. Defaults to ``%(expression)s OVER (%(window)s)``. If only the
  576. ``expression`` argument is provided, the window clause will be blank.
  577. The ``Window`` class is the main expression for an ``OVER`` clause.
  578. The ``expression`` argument is either a :ref:`window function
  579. <window-functions>`, an :ref:`aggregate function <aggregation-functions>`, or
  580. an expression that's compatible in a window clause.
  581. The ``partition_by`` argument accepts an expression or a sequence of
  582. expressions (column names should be wrapped in an ``F``-object) that control
  583. the partitioning of the rows. Partitioning narrows which rows are used to
  584. compute the result set.
  585. The :ref:`output_field<output-field>` is specified either as an argument or by
  586. the expression.
  587. The ``order_by`` argument accepts an expression on which you can call
  588. :meth:`~django.db.models.Expression.asc` and
  589. :meth:`~django.db.models.Expression.desc`, a string of a field name (with an
  590. optional ``"-"`` prefix which indicates descending order), or a tuple or list
  591. of strings and/or expressions. The ordering controls the order in which the
  592. expression is applied. For example, if you sum over the rows in a partition,
  593. the first result is the value of the first row, the second is the sum of first
  594. and second row.
  595. The ``frame`` parameter specifies which other rows that should be used in the
  596. computation. See :ref:`window-frames` for details.
  597. For example, to annotate each movie with the average rating for the movies by
  598. the same studio in the same genre and release year:
  599. .. code-block:: pycon
  600. >>> from django.db.models import Avg, F, Window
  601. >>> Movie.objects.annotate(
  602. ... avg_rating=Window(
  603. ... expression=Avg("rating"),
  604. ... partition_by=[F("studio"), F("genre")],
  605. ... order_by="released__year",
  606. ... ),
  607. ... )
  608. This allows you to check if a movie is rated better or worse than its peers.
  609. You may want to apply multiple expressions over the same window, i.e., the
  610. same partition and frame. For example, you could modify the previous example
  611. to also include the best and worst rating in each movie's group (same studio,
  612. genre, and release year) by using three window functions in the same query. The
  613. partition and ordering from the previous example is extracted into a dictionary
  614. to reduce repetition:
  615. .. code-block:: pycon
  616. >>> from django.db.models import Avg, F, Max, Min, Window
  617. >>> window = {
  618. ... "partition_by": [F("studio"), F("genre")],
  619. ... "order_by": "released__year",
  620. ... }
  621. >>> Movie.objects.annotate(
  622. ... avg_rating=Window(
  623. ... expression=Avg("rating"),
  624. ... **window,
  625. ... ),
  626. ... best=Window(
  627. ... expression=Max("rating"),
  628. ... **window,
  629. ... ),
  630. ... worst=Window(
  631. ... expression=Min("rating"),
  632. ... **window,
  633. ... ),
  634. ... )
  635. Filtering against window functions is supported as long as lookups are not
  636. disjunctive (not using ``OR`` or ``XOR`` as a connector) and against a queryset
  637. performing aggregation.
  638. For example, a query that relies on aggregation and has an ``OR``-ed filter
  639. against a window function and a field is not supported. Applying combined
  640. predicates post-aggregation could cause rows that would normally be excluded
  641. from groups to be included:
  642. .. code-block:: pycon
  643. >>> qs = Movie.objects.annotate(
  644. ... category_rank=Window(Rank(), partition_by="category", order_by="-rating"),
  645. ... scenes_count=Count("actors"),
  646. ... ).filter(Q(category_rank__lte=3) | Q(title__contains="Batman"))
  647. >>> list(qs)
  648. NotImplementedError: Heterogeneous disjunctive predicates against window functions
  649. are not implemented when performing conditional aggregation.
  650. Among Django's built-in database backends, MySQL, PostgreSQL, and Oracle
  651. support window expressions. Support for different window expression features
  652. varies among the different databases. For example, the options in
  653. :meth:`~django.db.models.Expression.asc` and
  654. :meth:`~django.db.models.Expression.desc` may not be supported. Consult the
  655. documentation for your database as needed.
  656. .. _window-frames:
  657. Frames
  658. ~~~~~~
  659. For a window frame, you can choose either a range-based sequence of rows or an
  660. ordinary sequence of rows.
  661. .. class:: ValueRange(start=None, end=None, exclusion=None)
  662. .. attribute:: frame_type
  663. This attribute is set to ``'RANGE'``.
  664. PostgreSQL has limited support for ``ValueRange`` and only supports use of
  665. the standard start and end points, such as ``CURRENT ROW`` and ``UNBOUNDED
  666. FOLLOWING``.
  667. .. versionchanged:: 5.1
  668. The ``exclusion`` argument was added.
  669. .. class:: RowRange(start=None, end=None, exclusion=None)
  670. .. attribute:: frame_type
  671. This attribute is set to ``'ROWS'``.
  672. .. versionchanged:: 5.1
  673. The ``exclusion`` argument was added.
  674. Both classes return SQL with the template:
  675. .. code-block:: sql
  676. %(frame_type)s BETWEEN %(start)s AND %(end)s
  677. .. class:: WindowFrameExclusion
  678. .. versionadded:: 5.1
  679. .. attribute:: CURRENT_ROW
  680. .. attribute:: GROUP
  681. .. attribute:: TIES
  682. .. attribute:: NO_OTHERS
  683. The ``exclusion`` argument allows excluding rows
  684. (:attr:`~WindowFrameExclusion.CURRENT_ROW`), groups
  685. (:attr:`~WindowFrameExclusion.GROUP`), and ties
  686. (:attr:`~WindowFrameExclusion.TIES`) from the window frames on supported
  687. databases:
  688. .. code-block:: sql
  689. %(frame_type)s BETWEEN %(start)s AND %(end)s EXCLUDE %(exclusion)s
  690. Frames narrow the rows that are used for computing the result. They shift from
  691. some start point to some specified end point. Frames can be used with and
  692. without partitions, but it's often a good idea to specify an ordering of the
  693. window to ensure a deterministic result. In a frame, a peer in a frame is a row
  694. with an equivalent value, or all rows if an ordering clause isn't present.
  695. The default starting point for a frame is ``UNBOUNDED PRECEDING`` which is the
  696. first row of the partition. The end point is always explicitly included in the
  697. SQL generated by the ORM and is by default ``UNBOUNDED FOLLOWING``. The default
  698. frame includes all rows from the partition to the last row in the set.
  699. The accepted values for the ``start`` and ``end`` arguments are ``None``, an
  700. integer, or zero. A negative integer for ``start`` results in ``N PRECEDING``,
  701. while ``None`` yields ``UNBOUNDED PRECEDING``. In ``ROWS`` mode, a positive
  702. integer can be used for ``start`` resulting in ``N FOLLOWING``. Positive
  703. integers are accepted for ``end`` and results in ``N FOLLOWING``. In ``ROWS``
  704. mode, a negative integer can be used for ``end`` resulting in ``N PRECEDING``.
  705. For both ``start`` and ``end``, zero will return ``CURRENT ROW``.
  706. There's a difference in what ``CURRENT ROW`` includes. When specified in
  707. ``ROWS`` mode, the frame starts or ends with the current row. When specified in
  708. ``RANGE`` mode, the frame starts or ends at the first or last peer according to
  709. the ordering clause. Thus, ``RANGE CURRENT ROW`` evaluates the expression for
  710. rows which have the same value specified by the ordering. Because the template
  711. includes both the ``start`` and ``end`` points, this may be expressed with::
  712. ValueRange(start=0, end=0)
  713. If a movie's "peers" are described as movies released by the same studio in the
  714. same genre in the same year, this ``RowRange`` example annotates each movie
  715. with the average rating of a movie's two prior and two following peers:
  716. .. code-block:: pycon
  717. >>> from django.db.models import Avg, F, RowRange, Window
  718. >>> Movie.objects.annotate(
  719. ... avg_rating=Window(
  720. ... expression=Avg("rating"),
  721. ... partition_by=[F("studio"), F("genre")],
  722. ... order_by="released__year",
  723. ... frame=RowRange(start=-2, end=2),
  724. ... ),
  725. ... )
  726. If the database supports it, you can specify the start and end points based on
  727. values of an expression in the partition. If the ``released`` field of the
  728. ``Movie`` model stores the release month of each movie, this ``ValueRange``
  729. example annotates each movie with the average rating of a movie's peers
  730. released between twelve months before and twelve months after each movie:
  731. .. code-block:: pycon
  732. >>> from django.db.models import Avg, F, ValueRange, Window
  733. >>> Movie.objects.annotate(
  734. ... avg_rating=Window(
  735. ... expression=Avg("rating"),
  736. ... partition_by=[F("studio"), F("genre")],
  737. ... order_by="released__year",
  738. ... frame=ValueRange(start=-12, end=12),
  739. ... ),
  740. ... )
  741. .. versionchanged:: 5.1
  742. Support for positive integer ``start`` and negative integer ``end`` was
  743. added for ``RowRange``.
  744. .. currentmodule:: django.db.models
  745. Technical Information
  746. =====================
  747. Below you'll find technical implementation details that may be useful to
  748. library authors. The technical API and examples below will help with
  749. creating generic query expressions that can extend the built-in functionality
  750. that Django provides.
  751. Expression API
  752. --------------
  753. Query expressions implement the :ref:`query expression API <query-expression>`,
  754. but also expose a number of extra methods and attributes listed below. All
  755. query expressions must inherit from ``Expression()`` or a relevant
  756. subclass.
  757. When a query expression wraps another expression, it is responsible for
  758. calling the appropriate methods on the wrapped expression.
  759. .. class:: Expression
  760. .. attribute:: allowed_default
  761. Tells Django that this expression can be used in
  762. :attr:`Field.db_default`. Defaults to ``False``.
  763. .. attribute:: constraint_validation_compatible
  764. .. versionadded:: 5.1
  765. Tells Django that this expression can be used during a constraint
  766. validation. Expressions with ``constraint_validation_compatible`` set
  767. to ``False`` must have only one source expression. Defaults to
  768. ``True``.
  769. .. attribute:: contains_aggregate
  770. Tells Django that this expression contains an aggregate and that a
  771. ``GROUP BY`` clause needs to be added to the query.
  772. .. attribute:: contains_over_clause
  773. Tells Django that this expression contains a
  774. :class:`~django.db.models.expressions.Window` expression. It's used,
  775. for example, to disallow window function expressions in queries that
  776. modify data.
  777. .. attribute:: filterable
  778. Tells Django that this expression can be referenced in
  779. :meth:`.QuerySet.filter`. Defaults to ``True``.
  780. .. attribute:: window_compatible
  781. Tells Django that this expression can be used as the source expression
  782. in :class:`~django.db.models.expressions.Window`. Defaults to
  783. ``False``.
  784. .. attribute:: empty_result_set_value
  785. Tells Django which value should be returned when the expression is used
  786. to apply a function over an empty result set. Defaults to
  787. :py:data:`NotImplemented` which forces the expression to be computed on
  788. the database.
  789. .. attribute:: set_returning
  790. .. versionadded:: 5.2
  791. Tells Django that this expression contains a set-returning function,
  792. enforcing subquery evaluation. It's used, for example, to allow some
  793. Postgres set-returning functions (e.g. ``JSONB_PATH_QUERY``,
  794. ``UNNEST``, etc.) to skip optimization and be properly evaluated when
  795. annotations spawn rows themselves. Defaults to ``False``.
  796. .. attribute:: allows_composite_expressions
  797. .. versionadded:: 5.2
  798. Tells Django that this expression allows composite expressions, for
  799. example, to support :ref:`composite primary keys
  800. <cpk-and-database-functions>`. Defaults to ``False``.
  801. .. method:: resolve_expression(query=None, allow_joins=True, reuse=None, summarize=False, for_save=False)
  802. Provides the chance to do any preprocessing or validation of
  803. the expression before it's added to the query. ``resolve_expression()``
  804. must also be called on any nested expressions. A ``copy()`` of ``self``
  805. should be returned with any necessary transformations.
  806. ``query`` is the backend query implementation.
  807. ``allow_joins`` is a boolean that allows or denies the use of
  808. joins in the query.
  809. ``reuse`` is a set of reusable joins for multi-join scenarios.
  810. ``summarize`` is a boolean that, when ``True``, signals that the
  811. query being computed is a terminal aggregate query.
  812. ``for_save`` is a boolean that, when ``True``, signals that the query
  813. being executed is performing a create or update.
  814. .. method:: get_source_expressions()
  815. Returns an ordered list of inner expressions. For example:
  816. .. code-block:: pycon
  817. >>> Sum(F("foo")).get_source_expressions()
  818. [F('foo')]
  819. .. method:: set_source_expressions(expressions)
  820. Takes a list of expressions and stores them such that
  821. ``get_source_expressions()`` can return them.
  822. .. method:: relabeled_clone(change_map)
  823. Returns a clone (copy) of ``self``, with any column aliases relabeled.
  824. Column aliases are renamed when subqueries are created.
  825. ``relabeled_clone()`` should also be called on any nested expressions
  826. and assigned to the clone.
  827. ``change_map`` is a dictionary mapping old aliases to new aliases.
  828. Example::
  829. def relabeled_clone(self, change_map):
  830. clone = copy.copy(self)
  831. clone.expression = self.expression.relabeled_clone(change_map)
  832. return clone
  833. .. method:: convert_value(value, expression, connection)
  834. A hook allowing the expression to coerce ``value`` into a more
  835. appropriate type.
  836. ``expression`` is the same as ``self``.
  837. .. method:: get_group_by_cols()
  838. Responsible for returning the list of columns references by
  839. this expression. ``get_group_by_cols()`` should be called on any
  840. nested expressions. ``F()`` objects, in particular, hold a reference
  841. to a column.
  842. .. method:: asc(nulls_first=None, nulls_last=None)
  843. Returns the expression ready to be sorted in ascending order.
  844. ``nulls_first`` and ``nulls_last`` define how null values are sorted.
  845. See :ref:`using-f-to-sort-null-values` for example usage.
  846. .. method:: desc(nulls_first=None, nulls_last=None)
  847. Returns the expression ready to be sorted in descending order.
  848. ``nulls_first`` and ``nulls_last`` define how null values are sorted.
  849. See :ref:`using-f-to-sort-null-values` for example usage.
  850. .. method:: reverse_ordering()
  851. Returns ``self`` with any modifications required to reverse the sort
  852. order within an ``order_by`` call. As an example, an expression
  853. implementing ``NULLS LAST`` would change its value to be
  854. ``NULLS FIRST``. Modifications are only required for expressions that
  855. implement sort order like ``OrderBy``. This method is called when
  856. :meth:`~django.db.models.query.QuerySet.reverse()` is called on a
  857. queryset.
  858. Writing your own Query Expressions
  859. ----------------------------------
  860. You can write your own query expression classes that use, and can integrate
  861. with, other query expressions. Let's step through an example by writing an
  862. implementation of the ``COALESCE`` SQL function, without using the built-in
  863. :ref:`Func() expressions <func-expressions>`.
  864. The ``COALESCE`` SQL function is defined as taking a list of columns or
  865. values. It will return the first column or value that isn't ``NULL``.
  866. We'll start by defining the template to be used for SQL generation and
  867. an ``__init__()`` method to set some attributes::
  868. import copy
  869. from django.db.models import Expression
  870. class Coalesce(Expression):
  871. template = "COALESCE( %(expressions)s )"
  872. def __init__(self, expressions, output_field):
  873. super().__init__(output_field=output_field)
  874. if len(expressions) < 2:
  875. raise ValueError("expressions must have at least 2 elements")
  876. for expression in expressions:
  877. if not hasattr(expression, "resolve_expression"):
  878. raise TypeError("%r is not an Expression" % expression)
  879. self.expressions = expressions
  880. We do some basic validation on the parameters, including requiring at least 2
  881. columns or values, and ensuring they are expressions. We are requiring
  882. :ref:`output_field<output-field>` here so that Django knows what kind of model
  883. field to assign the eventual result to.
  884. Now we implement the preprocessing and validation. Since we do not have
  885. any of our own validation at this point, we delegate to the nested
  886. expressions::
  887. def resolve_expression(
  888. self, query=None, allow_joins=True, reuse=None, summarize=False, for_save=False
  889. ):
  890. c = self.copy()
  891. c.is_summary = summarize
  892. for pos, expression in enumerate(self.expressions):
  893. c.expressions[pos] = expression.resolve_expression(
  894. query, allow_joins, reuse, summarize, for_save
  895. )
  896. return c
  897. Next, we write the method responsible for generating the SQL::
  898. def as_sql(self, compiler, connection, template=None):
  899. sql_expressions, sql_params = [], []
  900. for expression in self.expressions:
  901. sql, params = compiler.compile(expression)
  902. sql_expressions.append(sql)
  903. sql_params.extend(params)
  904. template = template or self.template
  905. data = {"expressions": ",".join(sql_expressions)}
  906. return template % data, sql_params
  907. def as_oracle(self, compiler, connection):
  908. """
  909. Example of vendor specific handling (Oracle in this case).
  910. Let's make the function name lowercase.
  911. """
  912. return self.as_sql(compiler, connection, template="coalesce( %(expressions)s )")
  913. ``as_sql()`` methods can support custom keyword arguments, allowing
  914. ``as_vendorname()`` methods to override data used to generate the SQL string.
  915. Using ``as_sql()`` keyword arguments for customization is preferable to
  916. mutating ``self`` within ``as_vendorname()`` methods as the latter can lead to
  917. errors when running on different database backends. If your class relies on
  918. class attributes to define data, consider allowing overrides in your
  919. ``as_sql()`` method.
  920. We generate the SQL for each of the ``expressions`` by using the
  921. ``compiler.compile()`` method, and join the result together with commas.
  922. Then the template is filled out with our data and the SQL and parameters
  923. are returned.
  924. We've also defined a custom implementation that is specific to the Oracle
  925. backend. The ``as_oracle()`` function will be called instead of ``as_sql()``
  926. if the Oracle backend is in use.
  927. Finally, we implement the rest of the methods that allow our query expression
  928. to play nice with other query expressions::
  929. def get_source_expressions(self):
  930. return self.expressions
  931. def set_source_expressions(self, expressions):
  932. self.expressions = expressions
  933. Let's see how it works:
  934. .. code-block:: pycon
  935. >>> from django.db.models import F, Value, CharField
  936. >>> qs = Company.objects.annotate(
  937. ... tagline=Coalesce(
  938. ... [F("motto"), F("ticker_name"), F("description"), Value("No Tagline")],
  939. ... output_field=CharField(),
  940. ... )
  941. ... )
  942. >>> for c in qs:
  943. ... print("%s: %s" % (c.name, c.tagline))
  944. ...
  945. Google: Do No Evil
  946. Apple: AAPL
  947. Yahoo: Internet Company
  948. Django Software Foundation: No Tagline
  949. .. _avoiding-sql-injection-in-query-expressions:
  950. Avoiding SQL injection
  951. ~~~~~~~~~~~~~~~~~~~~~~
  952. Since a ``Func``'s keyword arguments for ``__init__()`` (``**extra``) and
  953. ``as_sql()`` (``**extra_context``) are interpolated into the SQL string rather
  954. than passed as query parameters (where the database driver would escape them),
  955. they must not contain untrusted user input.
  956. For example, if ``substring`` is user-provided, this function is vulnerable to
  957. SQL injection::
  958. from django.db.models import Func
  959. class Position(Func):
  960. function = "POSITION"
  961. template = "%(function)s('%(substring)s' in %(expressions)s)"
  962. def __init__(self, expression, substring):
  963. # substring=substring is an SQL injection vulnerability!
  964. super().__init__(expression, substring=substring)
  965. This function generates an SQL string without any parameters. Since
  966. ``substring`` is passed to ``super().__init__()`` as a keyword argument, it's
  967. interpolated into the SQL string before the query is sent to the database.
  968. Here's a corrected rewrite::
  969. class Position(Func):
  970. function = "POSITION"
  971. arg_joiner = " IN "
  972. def __init__(self, expression, substring):
  973. super().__init__(substring, expression)
  974. With ``substring`` instead passed as a positional argument, it'll be passed as
  975. a parameter in the database query.
  976. Adding support in third-party database backends
  977. -----------------------------------------------
  978. If you're using a database backend that uses a different SQL syntax for a
  979. certain function, you can add support for it by monkey patching a new method
  980. onto the function's class.
  981. Let's say we're writing a backend for Microsoft's SQL Server which uses the SQL
  982. ``LEN`` instead of ``LENGTH`` for the :class:`~functions.Length` function.
  983. We'll monkey patch a new method called ``as_sqlserver()`` onto the ``Length``
  984. class::
  985. from django.db.models.functions import Length
  986. def sqlserver_length(self, compiler, connection):
  987. return self.as_sql(compiler, connection, function="LEN")
  988. Length.as_sqlserver = sqlserver_length
  989. You can also customize the SQL using the ``template`` parameter of ``as_sql()``.
  990. We use ``as_sqlserver()`` because ``django.db.connection.vendor`` returns
  991. ``sqlserver`` for the backend.
  992. Third-party backends can register their functions in the top level
  993. ``__init__.py`` file of the backend package or in a top level ``expressions.py``
  994. file (or package) that is imported from the top level ``__init__.py``.
  995. For user projects wishing to patch the backend that they're using, this code
  996. should live in an :meth:`AppConfig.ready()<django.apps.AppConfig.ready>` method.