1024programmer Java Using multiple conditions in Django’s CaseWhen expression

Using multiple conditions in Django’s CaseWhen expression

According to the Django documentation, multiple conditions can be used in the when clause.

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

However, I am not able to use the same when using Case clause.

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

I ended up getting the following error:

TypeError: __init__() got multiple values ​​for keyword argument 'then'

Is there any way to do this? Am I missing something here?

1> Ash..:


Maybe the 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'
 )
 

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/772196

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

The latest and most comprehensive programming knowledge, all in 1024programmer.com

© 2023 1024programmer - Encyclopedia of Programming Field
Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us