[Fixed]-Using multiple conditions in Django's Case When expressions

26👍

Maybe Q expression can help. Try this:

Case(
    When(
        Q(registered_on__gt=date(2014, 1, 1)) & Q(registered_on__lt=date(2015, 1, 1)),
        then='account_type'
    ),
    default='default'
)

2👍

As I see in the Django docs of Case expression, each When expression in the Case class has its own then parameters, and instead of put all the condition in only one parameter When, I think maybe you should extract into 2 When expressions, with 2 separate then parameters, as below:

    Case(
         When(
            registered_on__gt=date(2014, 1, 1),
            then = 'account_type',
         ),
         When(
            registered_on__lt=date(2015, 1, 1),
            then = 'account_type',
         ),
         default='default'
    )

Hope it helps. If anything else is unclear please let me know.
Cheer!

Leave a comment