mirror of
https://github.com/django/django.git
synced 2024-12-22 17:16:24 +00:00
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
|
||||
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")
|
||||
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)
|
||||
self.condition = condition
|
||||
self.result = self._parse_expressions(then)[0]
|
||||
|
@ -1301,3 +1301,8 @@ class CaseWhenTests(SimpleTestCase):
|
||||
When(condition=object())
|
||||
with self.assertRaisesMessage(TypeError, msg):
|
||||
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
Block a user