feincms / django-tree-queries

Adjacency-list trees for Django using recursive common table expressions. Supports PostgreSQL, sqlite, MySQL and MariaDB.
https://django-tree-queries.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
427 stars 27 forks source link

Is there a good way to access tree_depth on a single node/object? #31

Closed peter-stuart-turner closed 2 years ago

peter-stuart-turner commented 2 years ago

I am having to use the queryset method:

TreeNode.objects.filter(id=node.id).with_tree_fields().first().tree_depth

but is there an easier way to access - I am wanting to serialize it within Django Rest framework

matthiask commented 2 years ago

Hi

There's no easier way to know the position of the node in the tree without building the whole tree structure in the database. Not without denormalization.

peter-stuart-turner commented 2 years ago

Got it, thanks

matthiask commented 2 years ago

Sorry, what I wrote above isn't completely correct. A simpler recursive CTE for fetching the depth is possible of course but django-tree-queries doesn't have any support for this.

Thanks!