条件表达式¶
条件表达式允许您在过滤器,注释,聚合和更新中使用 if
... elif
... else
逻辑。条件表达式计算表的每一行的一系列条件,并返回匹配结果表达式。条件表达式也可以与其他 表达式 一样组合和嵌套。
条件表达式类¶
我们将在后面的示例中使用以下模型:
from django.db import models
class Client(models.Model):
REGULAR = 'R'
GOLD = 'G'
PLATINUM = 'P'
ACCOUNT_TYPE_CHOICES = (
(REGULAR, 'Regular'),
(GOLD, 'Gold'),
(PLATINUM, 'Platinum'),
)
name = models.CharField(max_length=50)
registered_on = models.DateField()
account_type = models.CharField(
max_length=1,
choices=ACCOUNT_TYPE_CHOICES,
default=REGULAR,
)
When
¶
When()
对象用于封装条件及其结果以在条件表达式中使用。使用 When()
对象类似于使用 filter()
方法。可以使用 字段查找 或 Q
对象指定条件。使用 then
关键字提供结果。
一些例子:
>>> from django.db.models import When, F, Q
>>> # String arguments refer to fields; the following two examples are equivalent:
>>> When(account_type=Client.GOLD, then='name')
>>> When(account_type=Client.GOLD, then=F('name'))
>>> # You can use field lookups in the condition
>>> from datetime import date
>>> When(registered_on__gt=date(2014, 1, 1),
... registered_on__lt=date(2015, 1, 1),
... then='account_type')
>>> # Complex conditions can be created using Q objects
>>> When(Q(name__startswith="John") | Q(name__startswith="Paul"),
... then='name')
请记住,每个值都可以是一个表达式。
注解
由于 then
关键字参数被保留用于 When()
的结果,如果 Model
具有名为 then
的字段,则存在潜在冲突。这可以通过两种方式解决:
>>> When(then__exact=0, then=1)
>>> When(Q(then=0), then=1)
Case
¶
Case()
表达式像 Python
中的 if
... elif
... else
语句。提供的 When()
对象中的每个 condition
按顺序评估,直到一个评估为真实值。返回来自匹配的 When()
对象的 result
表达式。
一个简单的例子:
>>>
>>> from datetime import date, timedelta
>>> from django.db.models import CharField, Case, Value, When
>>> Client.objects.create(
... name='Jane Doe',
... account_type=Client.REGULAR,
... registered_on=date.today() - timedelta(days=36))
>>> Client.objects.create(
... name='James Smith',
... account_type=Client.GOLD,
... registered_on=date.today() - timedelta(days=5))
>>> Client.objects.create(
... name='Jack Black',
... account_type=Client.PLATINUM,
... registered_on=date.today() - timedelta(days=10 * 365))
>>> # Get the discount for each Client based on the account type
>>> Client.objects.annotate(
... discount=Case(
... When(account_type=Client.GOLD, then=Value('5%')),
... When(account_type=Client.PLATINUM, then=Value('10%')),
... default=Value('0%'),
... output_field=CharField(),
... ),
... ).values_list('name', 'discount')
[('Jane Doe', '0%'), ('James Smith', '5%'), ('Jack Black', '10%')]
Case()
接受任意数量的 When()
对象作为单独的参数。使用关键字参数提供其他选项。如果没有条件评估为 TRUE
,则返回使用 default
关键字参数给出的表达式。如果没有提供 default
参数,则使用 None
。
如果我们想要改变我们以前的查询以获得基于 Client
与我们一起多久的折扣,我们可以使用查找:
>>> a_month_ago = date.today() - timedelta(days=30)
>>> a_year_ago = date.today() - timedelta(days=365)
>>> # Get the discount for each Client based on the registration date
>>> Client.objects.annotate(
... discount=Case(
... When(registered_on__lte=a_year_ago, then=Value('10%')),
... When(registered_on__lte=a_month_ago, then=Value('5%')),
... default=Value('0%'),
... output_field=CharField(),
... )
... ).values_list('name', 'discount')
[('Jane Doe', '5%'), ('James Smith', '0%'), ('Jack Black', '10%')]
注解
记住,条件是按顺序评估的,所以在上面的例子中,我们得到正确的结果,即使第二个条件匹配Jane Doe和Jack Black。这个工作就像一个 if
... elif
... else
语句在 Python
。
Case()
也在 filter()
子句中工作。例如,找到一个多月前注册的黄金客户和一年多前注册的铂金客户:
>>> a_month_ago = date.today() - timedelta(days=30)
>>> a_year_ago = date.today() - timedelta(days=365)
>>> Client.objects.filter(
... registered_on__lte=Case(
... When(account_type=Client.GOLD, then=a_month_ago),
... When(account_type=Client.PLATINUM, then=a_year_ago),
... ),
... ).values_list('name', 'account_type')
[('Jack Black', 'P')]
高级查询¶
条件表达式可用于注释,聚合,查找和更新。它们也可以与其他表达式组合和嵌套。这允许您进行强大的条件查询。
条件更新¶
假设我们要为客户更改 account_type
以符合其注册日期。我们可以使用条件表达式和 update()
方法来做到这一点:
>>> a_month_ago = date.today() - timedelta(days=30)
>>> a_year_ago = date.today() - timedelta(days=365)
>>> # Update the account_type for each Client from the registration date
>>> Client.objects.update(
... account_type=Case(
... When(registered_on__lte=a_year_ago,
... then=Value(Client.PLATINUM)),
... When(registered_on__lte=a_month_ago,
... then=Value(Client.GOLD)),
... default=Value(Client.REGULAR)
... ),
... )
>>> Client.objects.values_list('name', 'account_type')
[('Jane Doe', 'G'), ('James Smith', 'R'), ('Jack Black', 'P')]
条件聚合¶
如果我们想知道每个 account_type
有多少客户端怎么办?我们可以在 聚合函数 中嵌套条件表达式来实现这一点:
>>> # Create some more Clients first so we can have something to count
>>> Client.objects.create(
... name='Jean Grey',
... account_type=Client.REGULAR,
... registered_on=date.today())
>>> Client.objects.create(
... name='James Bond',
... account_type=Client.PLATINUM,
... registered_on=date.today())
>>> Client.objects.create(
... name='Jane Porter',
... account_type=Client.PLATINUM,
... registered_on=date.today())
>>> # Get counts for each value of account_type
>>> from django.db.models import IntegerField, Sum
>>> Client.objects.aggregate(
... regular=Sum(
... Case(When(account_type=Client.REGULAR, then=1),
... output_field=IntegerField())
... ),
... gold=Sum(
... Case(When(account_type=Client.GOLD, then=1),
... output_field=IntegerField())
... ),
... platinum=Sum(
... Case(When(account_type=Client.PLATINUM, then=1),
... output_field=IntegerField())
... )
... )
{'regular': 2, 'gold': 1, 'platinum': 3}