條件表達式

條件表達式允許你在篩選器、註解、聚合和更新中使用 ifelifelse 邏輯。條件表達式會針對表格的每一列評估一系列條件,並返回匹配的結果表達式。條件表達式也可以像其他表達式一樣組合和巢狀使用。

條件表達式類別

我們將在後續範例中使用以下模型

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)[原始碼]

When() 物件用於封裝條件及其結果,以便在條件表達式中使用。使用 When() 物件類似於使用 filter() 方法。可以使用欄位查詢Q 物件或具有 output_fieldBooleanFieldExpression 物件來指定條件。結果使用 then 關鍵字提供。

一些範例

>>> from django.db.models import F, Q, When
>>> # 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")
>>> # Condition can be created using boolean expressions.
>>> from django.db.models import Exists, OuterRef
>>> non_unique_account_type = (
...     Client.objects.filter(
...         account_type=OuterRef("account_type"),
...     )
...     .exclude(pk=OuterRef("pk"))
...     .values("pk")
... )
>>> When(Exists(non_unique_account_type), then=Value("non unique"))
>>> # Condition can be created using lookup expressions.
>>> from django.db.models.lookups import GreaterThan, LessThan
>>> When(
...     GreaterThan(F("registered_on"), date(2014, 1, 1))
...     & LessThan(F("registered_on"), date(2015, 1, 1)),
...     then="account_type",
... )

請記住,這些值中的每一個都可以是一個表達式。

注意

由於 then 關鍵字引數保留給 When() 的結果,如果 Model 有一個名為 then 的欄位,則可能會發生衝突。這可以透過兩種方式解決

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

Case

class Case(*cases, **extra)[原始碼]

Case() 表達式類似於 Python 中的 ifelifelse 陳述式。會依序評估提供的 When() 物件中的每個 condition,直到其中一個評估為真值。會返回匹配的 When() 物件中的 result 表達式。

一個範例

>>>
>>> from datetime import date, timedelta
>>> from django.db.models import 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%"),
...     ),
... ).values_list("name", "discount")
<QuerySet [('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%"),
...     )
... ).values_list("name", "discount")
<QuerySet [('Jane Doe', '5%'), ('James Smith', '0%'), ('Jack Black', '10%')]>

注意

請記住,條件是按順序評估的,因此在上面的範例中,即使第二個條件同時符合 Jane Doe 和 Jack Black,我們也會得到正確的結果。這就像 Python 中的 ifelifelse 陳述式一樣運作。

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")
<QuerySet [('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")
<QuerySet [('Jane Doe', 'G'), ('James Smith', 'R'), ('Jack Black', 'P')]>

條件聚合

如果我們想要找出每個 account_type 有多少客戶?我們可以使用聚合函式filter 引數來實現這一點

>>> # 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 Count
>>> Client.objects.aggregate(
...     regular=Count("pk", filter=Q(account_type=Client.REGULAR)),
...     gold=Count("pk", filter=Q(account_type=Client.GOLD)),
...     platinum=Count("pk", filter=Q(account_type=Client.PLATINUM)),
... )
{'regular': 2, 'gold': 1, 'platinum': 3}

此聚合會在使用 SQL 2003 FILTER WHERE 語法的資料庫上產生查詢,這些資料庫支援此語法

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

在其他資料庫上,這可以使用 CASE 陳述式來模擬

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

這兩個 SQL 陳述式在功能上是等效的,但更明確的 FILTER 可能效能更好。

條件篩選器

當條件表達式返回布林值時,可以直接在篩選器中使用。這表示它不會新增至 SELECT 欄位,但你仍然可以使用它來篩選結果

>>> non_unique_account_type = (
...     Client.objects.filter(
...         account_type=OuterRef("account_type"),
...     )
...     .exclude(pk=OuterRef("pk"))
...     .values("pk")
... )
>>> Client.objects.filter(~Exists(non_unique_account_type))

用 SQL 術語來說,這會評估為

SELECT ...
FROM client c0
WHERE NOT EXISTS (
  SELECT c1.id
  FROM client c1
  WHERE c1.account_type = c0.account_type AND NOT c1.id = c0.id
)
回到頂端