conditional-expressions.txt 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. =======================
  2. Conditional Expressions
  3. =======================
  4. .. currentmodule:: django.db.models.expressions
  5. Conditional expressions let you use :keyword:`if` ... :keyword:`elif` ...
  6. :keyword:`else` logic within filters, annotations, aggregations, and updates. A
  7. conditional expression evaluates a series of conditions for each row of a
  8. table and returns the matching result expression. Conditional expressions can
  9. also be combined and nested like other :doc:`expressions <expressions>`.
  10. The conditional expression classes
  11. ==================================
  12. We'll be using the following model in the subsequent examples::
  13. from django.db import models
  14. class Client(models.Model):
  15. REGULAR = 'R'
  16. GOLD = 'G'
  17. PLATINUM = 'P'
  18. ACCOUNT_TYPE_CHOICES = (
  19. (REGULAR, 'Regular'),
  20. (GOLD, 'Gold'),
  21. (PLATINUM, 'Platinum'),
  22. )
  23. name = models.CharField(max_length=50)
  24. registered_on = models.DateField()
  25. account_type = models.CharField(
  26. max_length=1,
  27. choices=ACCOUNT_TYPE_CHOICES,
  28. default=REGULAR,
  29. )
  30. ``When``
  31. --------
  32. .. class:: When(condition=None, then=None, **lookups)
  33. A ``When()`` object is used to encapsulate a condition and its result for use
  34. in the conditional expression. Using a ``When()`` object is similar to using
  35. the :meth:`~django.db.models.query.QuerySet.filter` method. The condition can
  36. be specified using :ref:`field lookups <field-lookups>` or
  37. :class:`~django.db.models.Q` objects. The result is provided using the ``then``
  38. keyword.
  39. Some examples::
  40. >>> from django.db.models import When, F, Q
  41. >>> # String arguments refer to fields; the following two examples are equivalent:
  42. >>> When(account_type=Client.GOLD, then='name')
  43. >>> When(account_type=Client.GOLD, then=F('name'))
  44. >>> # You can use field lookups in the condition
  45. >>> from datetime import date
  46. >>> When(registered_on__gt=date(2014, 1, 1),
  47. ... registered_on__lt=date(2015, 1, 1),
  48. ... then='account_type')
  49. >>> # Complex conditions can be created using Q objects
  50. >>> When(Q(name__startswith="John") | Q(name__startswith="Paul"),
  51. ... then='name')
  52. Keep in mind that each of these values can be an expression.
  53. .. note::
  54. Since the ``then`` keyword argument is reserved for the result of the
  55. ``When()``, there is a potential conflict if a
  56. :class:`~django.db.models.Model` has a field named ``then``. This can be
  57. resolved in two ways::
  58. >>> from django.db.models import Value
  59. >>> When(then__exact=0, then=1)
  60. >>> When(Q(then=0), then=1)
  61. ``Case``
  62. --------
  63. .. class:: Case(*cases, **extra)
  64. A ``Case()`` expression is like the :keyword:`if` ... :keyword:`elif` ...
  65. :keyword:`else` statement in ``Python``. Each ``condition`` in the provided
  66. ``When()`` objects is evaluated in order, until one evaluates to a
  67. truthful value. The ``result`` expression from the matching ``When()`` object
  68. is returned.
  69. A simple example::
  70. >>>
  71. >>> from datetime import date, timedelta
  72. >>> from django.db.models import CharField, Case, Value, When
  73. >>> Client.objects.create(
  74. ... name='Jane Doe',
  75. ... account_type=Client.REGULAR,
  76. ... registered_on=date.today() - timedelta(days=36))
  77. >>> Client.objects.create(
  78. ... name='James Smith',
  79. ... account_type=Client.GOLD,
  80. ... registered_on=date.today() - timedelta(days=5))
  81. >>> Client.objects.create(
  82. ... name='Jack Black',
  83. ... account_type=Client.PLATINUM,
  84. ... registered_on=date.today() - timedelta(days=10 * 365))
  85. >>> # Get the discount for each Client based on the account type
  86. >>> Client.objects.annotate(
  87. ... discount=Case(
  88. ... When(account_type=Client.GOLD, then=Value('5%')),
  89. ... When(account_type=Client.PLATINUM, then=Value('10%')),
  90. ... default=Value('0%'),
  91. ... output_field=CharField(),
  92. ... ),
  93. ... ).values_list('name', 'discount')
  94. [('Jane Doe', '0%'), ('James Smith', '5%'), ('Jack Black', '10%')]
  95. ``Case()`` accepts any number of ``When()`` objects as individual arguments.
  96. Other options are provided using keyword arguments. If none of the conditions
  97. evaluate to ``TRUE``, then the expression given with the ``default`` keyword
  98. argument is returned. If no ``default`` argument is provided, ``Value(None)``
  99. is used.
  100. If we wanted to change our previous query to get the discount based on how long
  101. the ``Client`` has been with us, we could do so using lookups::
  102. >>> a_month_ago = date.today() - timedelta(days=30)
  103. >>> a_year_ago = date.today() - timedelta(days=365)
  104. >>> # Get the discount for each Client based on the registration date
  105. >>> Client.objects.annotate(
  106. ... discount=Case(
  107. ... When(registered_on__lte=a_year_ago, then=Value('10%')),
  108. ... When(registered_on__lte=a_month_ago, then=Value('5%')),
  109. ... default=Value('0%'),
  110. ... output_field=CharField(),
  111. ... )
  112. ... ).values_list('name', 'discount')
  113. [('Jane Doe', '5%'), ('James Smith', '0%'), ('Jack Black', '10%')]
  114. .. note::
  115. Remember that the conditions are evaluated in order, so in the above
  116. example we get the correct result even though the second condition matches
  117. both Jane Doe and Jack Black. This works just like an :keyword:`if` ...
  118. :keyword:`elif` ... :keyword:`else` statement in ``Python``.
  119. Advanced queries
  120. ================
  121. Conditional expressions can be used in annotations, aggregations, lookups, and
  122. updates. They can also be combined and nested with other expressions. This
  123. allows you to make powerful conditional queries.
  124. Conditional update
  125. ------------------
  126. Let's say we want to change the ``account_type`` for our clients to match
  127. their registration dates. We can do this using a conditional expression and the
  128. :meth:`~django.db.models.query.QuerySet.update` method::
  129. >>> a_month_ago = date.today() - timedelta(days=30)
  130. >>> a_year_ago = date.today() - timedelta(days=365)
  131. >>> # Update the account_type for each Client from the registration date
  132. >>> Client.objects.update(
  133. ... account_type=Case(
  134. ... When(registered_on__lte=a_year_ago,
  135. ... then=Value(Client.PLATINUM)),
  136. ... When(registered_on__lte=a_month_ago,
  137. ... then=Value(Client.GOLD)),
  138. ... default=Value(Client.REGULAR)
  139. ... ),
  140. ... )
  141. >>> Client.objects.values_list('name', 'account_type')
  142. [('Jane Doe', 'G'), ('James Smith', 'R'), ('Jack Black', 'P')]
  143. Conditional aggregation
  144. -----------------------
  145. What if we want to find out how many clients there are for each
  146. ``account_type``? We can nest conditional expression within
  147. :ref:`aggregate functions <aggregation-functions>` to achieve this::
  148. >>> # Create some more Clients first so we can have something to count
  149. >>> Client.objects.create(
  150. ... name='Jean Grey',
  151. ... account_type=Client.REGULAR,
  152. ... registered_on=date.today())
  153. >>> Client.objects.create(
  154. ... name='James Bond',
  155. ... account_type=Client.PLATINUM,
  156. ... registered_on=date.today())
  157. >>> Client.objects.create(
  158. ... name='Jane Porter',
  159. ... account_type=Client.PLATINUM,
  160. ... registered_on=date.today())
  161. >>> # Get counts for each value of account_type
  162. >>> from django.db.models import IntegerField, Sum
  163. >>> Client.objects.aggregate(
  164. ... regular=Sum(
  165. ... Case(When(account_type=Client.REGULAR, then=1),
  166. ... output_field=IntegerField())
  167. ... ),
  168. ... gold=Sum(
  169. ... Case(When(account_type=Client.GOLD, then=1),
  170. ... output_field=IntegerField())
  171. ... ),
  172. ... platinum=Sum(
  173. ... Case(When(account_type=Client.PLATINUM, then=1),
  174. ... output_field=IntegerField())
  175. ... )
  176. ... )
  177. {'regular': 2, 'gold': 1, 'platinum': 3}