Question : case statement in querset django
Answered by : trained-tuna
# Creating a calculated column named 'discount' on Client table
# Using conditional case statement with nested When -> Then logic
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')
Source : https://docs.djangoproject.com/en/4.0/ref/models/conditional-expressions/ | Last Update : Thu, 03 Mar 22