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
415 stars 27 forks source link

A way to output tree fields on values() call #69

Open yakovyarmo opened 4 months ago

yakovyarmo commented 4 months ago

Hi, Is there any way to get tree_path and tree_depth on values() call? for example:

Node.objects.with_tree_fields().values()

or

Node.objects.with_tree_fields().values('tree_depth')

matthiask commented 4 months ago

Yeah, you can do something like

from django.db.models.expressions import RawSQL

Node.objects.with_tree_fields().values(
    tree_depth=RawSQL("tree_depth", ()), 
    tree_path=RawSQL("tree_path", ()),
)

Note though that this only returns an array for tree_path when using PostgreSQL, other DB engines will return the internal representation used by django-tree-queries because those DB engines do not support arrays as a native datatype. Also, the representation of those values is subject to change, so please don't rely on them too much. This has already bitten other people in the past, see #65