Fixed #28731 -- Added an error message when using an empty Q() in a When expression.
Otherwise it generates invalid SQL.
This commit is contained in:
parent
6deaddcca3
commit
5778b5701d
|
@ -818,6 +818,8 @@ class When(Expression):
|
||||||
condition, lookups = Q(**lookups), None
|
condition, lookups = Q(**lookups), None
|
||||||
if condition is None or not getattr(condition, 'conditional', False) or lookups:
|
if condition is None or not getattr(condition, 'conditional', False) or lookups:
|
||||||
raise TypeError("__init__() takes either a Q object or lookups as keyword arguments")
|
raise TypeError("__init__() takes either a Q object or lookups as keyword arguments")
|
||||||
|
if isinstance(condition, Q) and not condition:
|
||||||
|
raise ValueError("An empty Q() can't be used as a When() condition.")
|
||||||
super().__init__(output_field=None)
|
super().__init__(output_field=None)
|
||||||
self.condition = condition
|
self.condition = condition
|
||||||
self.result = self._parse_expressions(then)[0]
|
self.result = self._parse_expressions(then)[0]
|
||||||
|
|
|
@ -1301,3 +1301,8 @@ class CaseWhenTests(SimpleTestCase):
|
||||||
When(condition=object())
|
When(condition=object())
|
||||||
with self.assertRaisesMessage(TypeError, msg):
|
with self.assertRaisesMessage(TypeError, msg):
|
||||||
When()
|
When()
|
||||||
|
|
||||||
|
def test_empty_q_object(self):
|
||||||
|
msg = "An empty Q() can't be used as a When() condition."
|
||||||
|
with self.assertRaisesMessage(ValueError, msg):
|
||||||
|
When(Q(), then=Value(True))
|
||||||
|
|
Loading…
Reference in New Issue