Skip to content

Commit 67f957a

Browse files
committed
feat: q to bool
1 parent 179ea6f commit 67f957a

2 files changed

Lines changed: 14 additions & 0 deletions

File tree

tests/test_q.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,17 @@ def test_q_basic():
1919
assert q.join_type == "AND"
2020

2121

22+
def test_q_to_bool():
23+
q = Q(row="data")
24+
q_empty = Q()
25+
q_children = Q(Q(row="data"), Q(row="data"))
26+
q_children_empty = Q(Q(), Q())
27+
assert bool(q) is True
28+
assert bool(q_empty) is False
29+
assert bool(q_children) is True
30+
assert bool(q_children_empty) is False
31+
32+
2233
def test_q_compound():
2334
q1 = Q(moo="cow")
2435
q2 = Q(moo="bull")

tortoise/expressions.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,9 @@ def __eq__(self, other: object) -> bool:
326326
and self.filters == other.filters
327327
)
328328

329+
def __bool__(self) -> bool:
330+
return any((self.filters, *(children for children in self.children)))
331+
329332
def negate(self) -> None:
330333
"""
331334
Negates the current Q object. (mutation)

0 commit comments

Comments
 (0)