条件式—Djangoドキュメント

提供:Dev Guides
< DjangoDjango/docs/3.2.x/ref/models/conditional-expressions
移動先:案内検索

条件式

条件式を使用すると、フィルター、注釈、集計、および更新内で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 オブジェクト、または BooleanField [であるoutput_fieldを持つ Expression オブジェクトを使用して指定できます。 X161X]。 結果は、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'))

これらの値はそれぞれ式である可能性があることに注意してください。

ノート

thenキーワード引数はWhen()の結果用に予約されているため、モデルthenという名前のフィールドがある場合は競合が発生する可能性があります。 これは2つの方法で解決できます。

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

バージョン3.2で変更: lookupscondition引数を使用するためのサポートが追加されました。


Case

class Case(*cases, **extra)

Case()式は、Pythonifelifelseステートメントのようなものです。 提供された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%')]>

ノート

条件は順番に評価されるため、上記の例では、2番目の条件がJaneDoeとJackBlackの両方に一致していても、正しい結果が得られることに注意してください。 これは、Pythonifelifelseステートメントと同じように機能します。


Case()filter()句でも機能します。 たとえば、1か月以上前に登録したゴールドクライアントと1年以上前に登録したプラチナクライアントを検索するには、次のようにします。

>>> 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;

2つの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
)