Skip to content

Commit 397644d

Browse files
authored
Avoid fetching ancestors for nodes without parents. (#89)
1 parent e928627 commit 397644d

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

tree_queries/query.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ def ancestors(self, of, *, include_self=False):
100100
Returns ancestors of the given node ordered from the root of the tree
101101
towards deeper levels, optionally including the node itself
102102
"""
103+
if not include_self and of.parent_id is None:
104+
# Node without parent cannot have ancestors.
105+
return self.none()
106+
103107
if not hasattr(of, "tree_path"):
104108
of = self.with_tree_fields().get(pk=pk(of))
105109

0 commit comments

Comments
 (0)