summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--django/db/models/query_utils.py15
-rw-r--r--tests/queries/test_q.py2
2 files changed, 13 insertions, 4 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)
diff --git a/tests/queries/test_q.py b/tests/queries/test_q.py
index 52200b2ecf..e5fa0c1778 100644
--- a/tests/queries/test_q.py
+++ b/tests/queries/test_q.py
@@ -273,7 +273,7 @@ class QTests(SimpleTestCase):
)
def test_connector_validation(self):
- msg = f"_connector must be one of {Q.AND!r}, {Q.OR!r}, {Q.XOR!r}, or None."
+ msg = f"connector must be one of {Q.AND!r}, {Q.OR!r}, {Q.XOR!r}, or None."
with self.assertRaisesMessage(ValueError, msg):
Q(_connector="evil")