Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions django/db/models/query_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion tests/queries/test_q.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ def test_create_helper(self):
)

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")

Expand Down
Loading