条件表达式

条件表达式让你可以在过滤器、注解、聚合和更新中使用 ifelifelse 逻辑。条件表达式对表的每条记录执行一系列条件,并返回匹配的结果表达式。条件表达式也可以像其他 表达式 一样进行组合和嵌套。

条件表达式类

在后续的例子中,我们将使用以下模型:

  1. from django.db import models
  2. class Client(models.Model):
  3. REGULAR = "R"
  4. GOLD = "G"
  5. PLATINUM = "P"
  6. ACCOUNT_TYPE_CHOICES = {
  7. REGULAR: "Regular",
  8. GOLD: "Gold",
  9. PLATINUM: "Platinum",
  10. }
  11. name = models.CharField(max_length=50)
  12. registered_on = models.DateField()
  13. account_type = models.CharField(
  14. max_length=1,
  15. choices=ACCOUNT_TYPE_CHOICES,
  16. default=REGULAR,
  17. )

When

class When(condition=None, then=None, **lookups)

When() 对象用于封装一个条件及其结果,以便在条件表达式中使用。使用 When() 对象类似于使用 filter() 方法。可以使用 字段查找Q 对象或 Expression 对象来指定条件,这些对象的 output_field 是 BooleanField。结果是用 then 关键字提供的。

Some examples:

  1. >>> from django.db.models import F, Q, When
  2. >>> # String arguments refer to fields; the following two examples are equivalent:
  3. >>> When(account_type=Client.GOLD, then="name")
  4. >>> When(account_type=Client.GOLD, then=F("name"))
  5. >>> # You can use field lookups in the condition
  6. >>> from datetime import date
  7. >>> When(
  8. ... registered_on__gt=date(2014, 1, 1),
  9. ... registered_on__lt=date(2015, 1, 1),
  10. ... then="account_type",
  11. ... )
  12. >>> # Complex conditions can be created using Q objects
  13. >>> When(Q(name__startswith="John") | Q(name__startswith="Paul"), then="name")
  14. >>> # Condition can be created using boolean expressions.
  15. >>> from django.db.models import Exists, OuterRef
  16. >>> non_unique_account_type = (
  17. ... Client.objects.filter(
  18. ... account_type=OuterRef("account_type"),
  19. ... )
  20. ... .exclude(pk=OuterRef("pk"))
  21. ... .values("pk")
  22. ... )
  23. >>> When(Exists(non_unique_account_type), then=Value("non unique"))
  24. >>> # Condition can be created using lookup expressions.
  25. >>> from django.db.models.lookups import GreaterThan, LessThan
  26. >>> When(
  27. ... GreaterThan(F("registered_on"), date(2014, 1, 1))
  28. ... & LessThan(F("registered_on"), date(2015, 1, 1)),
  29. ... then="account_type",
  30. ... )

请记住,每个值都可以是一个表达式。

备注

Since the then keyword argument is reserved for the result of the When(), there is a potential conflict if a Model has a field named then. This can be resolved in two ways:

  1. >>> When(then__exact=0, then=1)
  2. >>> When(Q(then=0), then=1)

Case

class Case(*cases, **extra)

Case() 表达式就像 Python 中的 ifelifelse 语句。在提供的 When() 对象中的每个 condition 按顺序执行,直到执行出一个对的值。从匹配的 When() 对象中返回 result 表达式。

An example:

  1. >>>
  2. >>> from datetime import date, timedelta
  3. >>> from django.db.models import Case, Value, When
  4. >>> Client.objects.create(
  5. ... name="Jane Doe",
  6. ... account_type=Client.REGULAR,
  7. ... registered_on=date.today() - timedelta(days=36),
  8. ... )
  9. >>> Client.objects.create(
  10. ... name="James Smith",
  11. ... account_type=Client.GOLD,
  12. ... registered_on=date.today() - timedelta(days=5),
  13. ... )
  14. >>> Client.objects.create(
  15. ... name="Jack Black",
  16. ... account_type=Client.PLATINUM,
  17. ... registered_on=date.today() - timedelta(days=10 * 365),
  18. ... )
  19. >>> # Get the discount for each Client based on the account type
  20. >>> Client.objects.annotate(
  21. ... discount=Case(
  22. ... When(account_type=Client.GOLD, then=Value("5%")),
  23. ... When(account_type=Client.PLATINUM, then=Value("10%")),
  24. ... default=Value("0%"),
  25. ... ),
  26. ... ).values_list("name", "discount")
  27. <QuerySet [('Jane Doe', '0%'), ('James Smith', '5%'), ('Jack Black', '10%')]>

Case() 接受任意数量的 When() 对象作为单个参数。其他选项是通过关键字参数提供的。如果没有一个条件的值是 TRUE,那么将返回用 default 关键字参数给出的表达式。如果没有提供 default 参数,则使用 None

If we wanted to change our previous query to get the discount based on how long the Client has been with us, we could do so using lookups:

  1. >>> a_month_ago = date.today() - timedelta(days=30)
  2. >>> a_year_ago = date.today() - timedelta(days=365)
  3. >>> # Get the discount for each Client based on the registration date
  4. >>> Client.objects.annotate(
  5. ... discount=Case(
  6. ... When(registered_on__lte=a_year_ago, then=Value("10%")),
  7. ... When(registered_on__lte=a_month_ago, then=Value("5%")),
  8. ... default=Value("0%"),
  9. ... )
  10. ... ).values_list("name", "discount")
  11. <QuerySet [('Jane Doe', '5%'), ('James Smith', '0%'), ('Jack Black', '10%')]>

备注

请记住,条件是按顺序计算的,所以在上面的例子中,尽管第二个条件同时符合 Jane Doe 和 Jack Black,我们还是得到了正确的结果。这就像在 Python 中的 ifelifelse 语句一样。

Case() also works in a filter() clause. For example, to find gold clients that registered more than a month ago and platinum clients that registered more than a year ago:

  1. >>> a_month_ago = date.today() - timedelta(days=30)
  2. >>> a_year_ago = date.today() - timedelta(days=365)
  3. >>> Client.objects.filter(
  4. ... registered_on__lte=Case(
  5. ... When(account_type=Client.GOLD, then=a_month_ago),
  6. ... When(account_type=Client.PLATINUM, then=a_year_ago),
  7. ... ),
  8. ... ).values_list("name", "account_type")
  9. <QuerySet [('Jack Black', 'P')]>

高级查询

条件表达式可用于注解、聚合、过滤器、查找和更新中。它们还可以与其他表达式组合和嵌套。这使你可以进行强大的条件查询。

条件更新

Let’s say we want to change the account_type for our clients to match their registration dates. We can do this using a conditional expression and the update() method:

  1. >>> a_month_ago = date.today() - timedelta(days=30)
  2. >>> a_year_ago = date.today() - timedelta(days=365)
  3. >>> # Update the account_type for each Client from the registration date
  4. >>> Client.objects.update(
  5. ... account_type=Case(
  6. ... When(registered_on__lte=a_year_ago, then=Value(Client.PLATINUM)),
  7. ... When(registered_on__lte=a_month_ago, then=Value(Client.GOLD)),
  8. ... default=Value(Client.REGULAR),
  9. ... ),
  10. ... )
  11. >>> Client.objects.values_list("name", "account_type")
  12. <QuerySet [('Jane Doe', 'G'), ('James Smith', 'R'), ('Jack Black', 'P')]>

条件聚合

What if we want to find out how many clients there are for each account_type? We can use the filter argument of aggregate functions to achieve this:

  1. >>> # Create some more Clients first so we can have something to count
  2. >>> Client.objects.create(
  3. ... name="Jean Grey", account_type=Client.REGULAR, registered_on=date.today()
  4. ... )
  5. >>> Client.objects.create(
  6. ... name="James Bond", account_type=Client.PLATINUM, registered_on=date.today()
  7. ... )
  8. >>> Client.objects.create(
  9. ... name="Jane Porter", account_type=Client.PLATINUM, registered_on=date.today()
  10. ... )
  11. >>> # Get counts for each value of account_type
  12. >>> from django.db.models import Count
  13. >>> Client.objects.aggregate(
  14. ... regular=Count("pk", filter=Q(account_type=Client.REGULAR)),
  15. ... gold=Count("pk", filter=Q(account_type=Client.GOLD)),
  16. ... platinum=Count("pk", filter=Q(account_type=Client.PLATINUM)),
  17. ... )
  18. {'regular': 2, 'gold': 1, 'platinum': 3}

在支持 SQL 2003 FILTER WHERE 语法的数据库上,这个聚合产生一个查询。

  1. SELECT count('id') FILTER (WHERE account_type=1) as regular,
  2. count('id') FILTER (WHERE account_type=2) as gold,
  3. count('id') FILTER (WHERE account_type=3) as platinum
  4. FROM clients;

在其他数据库中,这是用 CASE 语句模拟的:

  1. SELECT count(CASE WHEN account_type=1 THEN id ELSE null) as regular,
  2. count(CASE WHEN account_type=2 THEN id ELSE null) as gold,
  3. count(CASE WHEN account_type=3 THEN id ELSE null) as platinum
  4. FROM clients;

这两条 SQL 语句在功能上是等同的,但更明确的 FILTER 可能表现得更好。

条件过滤

When a conditional expression returns a boolean value, it is possible to use it directly in filters. This means that it will not be added to the SELECT columns, but you can still use it to filter results:

  1. >>> non_unique_account_type = (
  2. ... Client.objects.filter(
  3. ... account_type=OuterRef("account_type"),
  4. ... )
  5. ... .exclude(pk=OuterRef("pk"))
  6. ... .values("pk")
  7. ... )
  8. >>> Client.objects.filter(~Exists(non_unique_account_type))

用 SQL 术语来说,它的值是:

  1. SELECT ...
  2. FROM client c0
  3. WHERE NOT EXISTS (
  4. SELECT c1.id
  5. FROM client c1
  6. WHERE c1.account_type = c0.account_type AND NOT c1.id = c0.id
  7. )