dorm.tree¶
Helpers de adjacency-list / recursive CTEs.
Recetas en Recursive CTE.
API¶
dorm.tree.descendants_cte(model: Any, *, parent_field: str, root_pk: Any, fields: list[str] | None = None, pk_column: str = 'id', cte_name: str = 'descendants', cycle_field: str | None = None) -> CTE
¶
Build a :class:CTE whose rows are every descendant of
root_pk in model's adjacency list (rooted at the row whose
PK equals root_pk).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Any
|
the dorm Model whose table backs the tree. |
required |
parent_field
|
str
|
the self-referential FK column (typically the
|
required |
root_pk
|
Any
|
the PK value to start the walk from. |
required |
fields
|
list[str] | None
|
columns to project. Defaults to |
None
|
pk_column
|
str
|
the PK column name. Defaults to |
'id'
|
cte_name
|
str
|
identifier for the CTE inside the surrounding
statement. Defaults to |
'descendants'
|
cycle_field
|
str | None
|
when set, adds a |
None
|
dorm.tree.ancestors_cte(model: Any, *, parent_field: str, leaf_pk: Any, fields: list[str] | None = None, pk_column: str = 'id', cte_name: str = 'ancestors') -> CTE
¶
Build a recursive CTE that walks upward from leaf_pk
along parent_field until parent_field is NULL.
See :func:descendants_cte for argument semantics.
dorm.tree.descendants(model: Any, *, parent_field: str, root_pk: Any, pk_column: str = 'id', using: str = 'default')
¶
Convenience: return a list of dicts {pk_column, parent_field}
representing every descendant of root_pk. Equivalent to
building :func:descendants_cte and consuming it via raw SQL.
For richer column projections, build the CTE directly and stitch
it into your own with_cte()-shaped queryset.
dorm.tree.ancestors(model: Any, *, parent_field: str, leaf_pk: Any, pk_column: str = 'id', using: str = 'default')
¶
Convenience inverse of :func:descendants. Returns rows from
the leaf upward (root last).