diff --git a/tests/test_q.py b/tests/test_q.py index 28bad59c3..62422dcb1 100644 --- a/tests/test_q.py +++ b/tests/test_q.py @@ -19,6 +19,17 @@ def test_q_basic(): assert q.join_type == "AND" +def test_q_to_bool(): + q = Q(row="data") + q_empty = Q() + q_children = Q(Q(row="data"), Q(row="data")) + q_children_empty = Q(Q(), Q()) + assert bool(q) is True + assert bool(q_empty) is False + assert bool(q_children) is True + assert bool(q_children_empty) is False + + def test_q_compound(): q1 = Q(moo="cow") q2 = Q(moo="bull") diff --git a/tortoise/expressions.py b/tortoise/expressions.py index 95b9b7d52..a01b98032 100644 --- a/tortoise/expressions.py +++ b/tortoise/expressions.py @@ -326,6 +326,9 @@ def __eq__(self, other: object) -> bool: and self.filters == other.filters ) + def __bool__(self) -> bool: + return any((self.filters, *(children for children in self.children))) + def negate(self) -> None: """ Negates the current Q object. (mutation)