

Other options are provided using keyword arguments. values_list ( "name", "discount" ) Ĭase() accepts any number of When() objects as individual arguments. ) > # Get the discount for each Client based on the account type > Client. today () - timedelta ( days = 10 * 365 ). > from datetime import date, timedelta > from django.db.models import Case, Value, When > Client.

Keep in mind that each of these values can be an expression. > from django.db.models.lookups import GreaterThan, LessThan > When (. ) > When ( Exists ( non_unique_account_type ), then = Value ( "non unique" )) > # Condition can be created using lookup expressions. account_type = OuterRef ( "account_type" ). > from django.db.models import Exists, OuterRef > non_unique_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. GOLD, then = F ( "name" )) > # You can use field lookups in the condition > from datetime import date > When (. GOLD, then = "name" ) > When ( account_type = Client. from django.db.models import F, Q, When > # String arguments refer to fields the following two examples are equivalent: > When ( account_type = Client.
