Django Conditional Expressions in Queries

Django Conditional Expressions are added in Django 1.8.

By using Conditional Expressions we can use "If...Elif...Else" expressions while querying the database.

Conditional expressions executes series of conditions while querying the database, It checks the condition for every record of the table in database and returns the matching results.

Conditional expressions can be nested and also can be combined.

The following are the Conditional Expressions in Django and Consider the below model for sample queries

class Employee(models.Model):
    ACCOUNT_TYPE_CHOICES = (
        ("REGULAR", 'Regular'),
        ("GOLD", 'Gold'),
        ("PLATINUM", 'Platinum'),
    )
    name = models.CharField(max_length=50)
    joined_on = models.DateField()
    salary = models.DecimalField()
    account_type = models.CharField(
        max_length=10,
        choices=ACCOUNT_TYPE_CHOICES,
        default="REGULAR",
    )

1. WHEN

A When() object is used as a condition inside the query

from django.db.models import When, F, Q

>>> When(field_name1_on__gt=date(2014, 1, 1), then="field_name2") # if we want the value in the field
>>> When(field_name1_on__gt=date(2014, 1, 1), then=5) # we can specify external value in place of "5"
>>> When(Q(name__startswith="John") | Q(name__startswith="Paul"), then="name") # we can also use nested lookups

2.CASE

A Case() expression is like the if ... elif ... else statement in Python. It executes the conditions one by one until one of the given conditions are satisfied. If no conditions are satisfied then the default value is returned if it is provided otherwise "None" will be returned.

from django.db.models import CharField, Case, Value, When

>>>ModelName.objects.annotate(
...     field=Case(
...         When(field1="value1", then=Value('5%')),
...         When(field1="value2", then=Value('10%')),
...         default=Value('0%'),
...         output_field=CharField(),
...     ),
... )

If we want to update the account type to PLATINUM if employee has more than 3 years of experiance, to GOLD if employee has more than 2 years of experiance otherwise REGULAR

For this we can write the code in two ways:

case1: Better Query without using conditional Expressions

>>> above_3yrs = date.today() - timedelta(days=365 * 3)
>>> above_2yrs = date.today() - timedelta(days=365 * 2)

>>> Employee.objects.filter(joined_on__lt=above_3yrs).update(account_type="PLATINUM")

>>> Employee.objects.filter(joined_on__lt=above_2yrs).update(account_type="GOLD")

Above code hits the database for two times to apply the change

case2: Best Query using conditional Expressions

>>> from datetime import date
>>> above_3yrs = date.today() - timedelta(days=365 * 3)
>>> above_2yrs = date.today() - timedelta(days=365 * 2)
>>> Employee.objects.update(
...     account_type=Case(
...         When(joined_on__lte=above_3yrs,
...              then=Value("PLATINUM")),
...         When(joined_on__lte=above_2yrs,
...              then=Value("GOLD")),
...         default=Value("REGULAR")
...     ),
... )

It hits the database only one time. So, We can reduce the no of queries to the database. By reducing the number of queries on the database, ultimately we can improve the efficiency and response time.

Posted On 13 February 2016 By MicroPyramid


Need any Help in your Project?Let's Talk

Latest Comments
Django Subdomains to do advanced things

We always struggle to give users customization's even before they login to the system like abc.micropyramid.com and django don't know how to handle that out ...

Continue Reading...
Django - Database access optimization

Django - Database access optimization, Django Queryset is generally lazy in nature. It will not hit the database until it evaluates the query results.

Continue Reading...
Django acceptance testing automation with robot framework

Acceptance testing automation for django web and REST applications using a robot testing framework.

Continue Reading...

Subscribe To our news letter

Subscribe and Stay Updated about our Webinars, news and articles on Django, Python, Machine Learning, Amazon Web Services, DevOps, Salesforce, ReactJS, AngularJS, React Native.
* We don't provide your email contact details to any third parties