Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
yakovyarmo opened this issue May 7, 2024 · 1 comment
Open

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

yakovyarmo opened this issue May 7, 2024 · 1 comment

Comments

@yakovyarmo
Copy link

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
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants