summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
authorAnna Makarudze <amakarudze@gmail.com>2026-03-21 19:49:02 +0100
committerSarah Boyce <42296566+sarahboyce@users.noreply.github.com>2026-04-28 12:42:49 +0200
commitfb292a549371a7c011c25b1a10fe5d25c579814a (patch)
tree61c85926d2830bb15a4912c451a9bcee2a789ab9 /django
parent32a3b4aa695e76ced1b2829b178e630b9ac62dce (diff)
Fixed #36912 -- Added connector validation to Q.create().
Co-authored-by: Jacob Walls <jacobtylerwalls@gmail.com>
Diffstat (limited to 'django')
-rw-r--r--django/db/models/query_utils.py15
1 files changed, 12 insertions, 3 deletions
diff --git a/django/db/models/query_utils.py b/django/db/models/query_utils.py
index c37e6b7a49..279e153ac1 100644
--- a/django/db/models/query_utils.py
+++ b/django/db/models/query_utils.py
@@ -53,15 +53,24 @@ class Q(tree.Node):
connectors = (None, AND, OR, XOR)
def __init__(self, *args, _connector=None, _negated=False, **kwargs):
- if _connector not in self.connectors:
- connector_reprs = ", ".join(f"{conn!r}" for conn in self.connectors[1:])
- raise ValueError(f"_connector must be one of {connector_reprs}, or None.")
+ self._check_connector(_connector)
super().__init__(
children=[*args, *sorted(kwargs.items())],
connector=_connector,
negated=_negated,
)
+ @classmethod
+ def create(cls, children=None, connector=None, negated=False):
+ cls._check_connector(connector)
+ return super().create(children=children, connector=connector, negated=negated)
+
+ @classmethod
+ def _check_connector(cls, connector):
+ if connector not in cls.connectors:
+ connector_reprs = ", ".join(f"{conn!r}" for conn in cls.connectors[1:])
+ raise ValueError(f"connector must be one of {connector_reprs}, or None.")
+
def _combine(self, other, conn):
if getattr(other, "conditional", False) is False:
raise TypeError(other)