Conditional Expressions

    We'll be using the following model in the subsequent examples:

    • class When(condition=None, then=None, **lookups)[源代码]
    • A When() object is used to encapsulate a condition and its result for usein the conditional expression. Using a When() object is similar to usingthe filter() method. The condition canbe specified using orQ objects. The result is provided using the thenkeyword.

    Some examples:

    1. >>> from django.db.models import F, Q, When
    2. >>> # String arguments refer to fields; the following two examples are equivalent:
    3. >>> When(account_type=Client.GOLD, then='name')
    4. >>> When(account_type=Client.GOLD, then=F('name'))
    5. >>> # You can use field lookups in the condition
    6. >>> from datetime import date
    7. >>> When(registered_on__gt=date(2014, 1, 1),
    8. ... registered_on__lt=date(2015, 1, 1),
    9. ... then='account_type')
    10. >>> # Complex conditions can be created using Q objects
    11. >>> When(Q(name__startswith="John") | Q(name__startswith="Paul"),
    12. ... then='name')

    Keep in mind that each of these values can be an expression.

    注解

    Since the then keyword argument is reserved for the result of theWhen(), there is a potential conflict if a has a field named then. This can beresolved in two ways:

    1. >>> When(then__exact=0, then=1)
    2. >>> When(Q(then=0), then=1)
    • A expression is like the if … …else statement in Python. Each condition in the providedWhen() objects is evaluated in order, until one evaluates to atruthful value. The result expression from the matching When() objectis returned.

    Case() accepts any number of When() objects as individual arguments.Other options are provided using keyword arguments. If none of the conditionsevaluate to TRUE, then the expression given with the default keywordargument is returned. If a default argument isn't provided, isused.

    If we wanted to change our previous query to get the discount based on how longthe Client has been with us, we could do so using lookups:

    1. >>> a_month_ago = date.today() - timedelta(days=30)
    2. >>> a_year_ago = date.today() - timedelta(days=365)
    3. >>> # Get the discount for each Client based on the registration date
    4. >>> Client.objects.annotate(
    5. ... discount=Case(
    6. ... When(registered_on__lte=a_year_ago, then=Value('10%')),
    7. ... When(registered_on__lte=a_month_ago, then=Value('5%')),
    8. ... default=Value('0%'),
    9. ... output_field=CharField(),
    10. ... )
    11. ... ).values_list('name', 'discount')
    12. <QuerySet [('Jane Doe', '5%'), ('James Smith', '0%'), ('Jack Black', '10%')]>

    注解

    Remember that the conditions are evaluated in order, so in the aboveexample we get the correct result even though the second condition matchesboth Jane Doe and Jack Black. This works just like an …elif … statement in Python.

    Case() also works in a filter() clause. For example, to find goldclients that registered more than a month ago and platinum clients thatregistered more than a year ago:

    1. >>> a_month_ago = date.today() - timedelta(days=30)
    2. >>> a_year_ago = date.today() - timedelta(days=365)
    3. >>> Client.objects.filter(
    4. ... registered_on__lte=Case(
    5. ... When(account_type=Client.GOLD, then=a_month_ago),
    6. ... When(account_type=Client.PLATINUM, then=a_year_ago),
    7. ... ),
    8. ... ).values_list('name', 'account_type')
    9. <QuerySet [('Jack Black', 'P')]>

    Advanced queries

    Let's say we want to change the account_type for our clients to matchtheir registration dates. We can do this using a conditional expression and the method:

    What if we want to find out how many clients there are for eachaccount_type? We can use the filter argument of aggregatefunctions to achieve this:

    1. >>> Client.objects.create(
    2. ... name='Jean Grey',
    3. ... account_type=Client.REGULAR,
    4. ... registered_on=date.today())
    5. >>> Client.objects.create(
    6. ... name='James Bond',
    7. ... account_type=Client.PLATINUM,
    8. ... registered_on=date.today())
    9. >>> Client.objects.create(
    10. ... name='Jane Porter',
    11. ... account_type=Client.PLATINUM,
    12. ... registered_on=date.today())
    13. >>> # Get counts for each value of account_type
    14. >>> from django.db.models import Count
    15. >>> Client.objects.aggregate(
    16. ... regular=Count('pk', filter=Q(account_type=Client.REGULAR)),
    17. ... gold=Count('pk', filter=Q(account_type=Client.GOLD)),
    18. ... platinum=Count('pk', filter=Q(account_type=Client.PLATINUM)),
    19. ... )
    20. {'regular': 2, 'gold': 1, 'platinum': 3}

    This aggregate produces a query with the SQL 2003 FILTER WHERE syntaxon databases that support it:

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

    On other databases, this is emulated using a CASE statement:

    The two SQL statements are functionally equivalent but the more explicit may perform better.