條件表達式
New in Django 1.8.
條件表達式允許你在過濾器、注解、聚合和更新操作中使用
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
class When(condition=None, then=None, **lookups)[source]
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的字段,會有潛在的沖突。這可以用以下兩種辦法解決:
>>> from django.db.models import Value
>>> When(then__exact=0, then=1)
>>> When(Q(then=0), then=1)
Case
class Case(*cases, **extra)[source]
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
參數,會使用
Value(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,我們也得到了正确的結果。這就像Python中的if … elif … else語句一樣。
進階查詢
條件表達式可以用于注解、聚合、查找和更新。它們也可以和其它表達式混合和嵌套。這可以讓你構造更強大的條件查詢。
條件更新
假設我們想要為用戶端修改
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}
譯者: Django 文檔協作翻譯小組 ,原文: Conditional Expressions 本文以 CC BY-NC-SA 3.0 協定釋出,轉載請保留作者署名和文章出處。 人手緊缺,有興趣的朋友可以加入我們,完全公益性質。交流群:467338606。