Fixed #31606 -- Allowed using condition with lookups in When() expression.

This commit is contained in:
Ryan Heard 2020-05-19 00:47:56 -05:00 committed by Mariusz Felisiak
parent 2aac176e86
commit 587b179d41
4 changed files with 26 additions and 3 deletions

View file

@ -876,8 +876,11 @@ class When(Expression):
conditional = False
def __init__(self, condition=None, then=None, **lookups):
if lookups and condition is None:
condition, lookups = Q(**lookups), None
if lookups:
if condition is None:
condition, lookups = Q(**lookups), None
elif getattr(condition, 'conditional', False):
condition, lookups = Q(condition, **lookups), None
if condition is None or not getattr(condition, 'conditional', False) or lookups:
raise TypeError(
'When() supports a Q object, a boolean expression, or lookups '