https://docs.djangoproject.com/en/2.2/ref/models/conditional-expressions/
# Case()接受任意数量的When()对象作为单独的参数。使用关键字参数提供其他选项。如果没有任何条件计算为TRUE,则default返回带有关键字参数的表达式。如果default未提供参数,None则使用。
# 示例
from datetime import date, timedelta
from django.db.models import Case, CharField, 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))
>>> 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(),
),
).value_list('name', 'discount')
<QuerySet [('Jane Doe', '0%'), ('James Smith', '5%'), ('Jack Black', '10%')]>