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 returning nodes in breadth-first order possible #4

Closed atodorov closed 4 years ago

atodorov commented 4 years ago

I want to generate a nested HTML list structure and the easiest way would be to have the tree nodes sorted in BFS order. Or be able to .order_by('tree_depth') on the resulting query.

Instead I can call .order_by('__tree.tree_depth') but that seems to have no effect.

matthiask commented 4 years ago

The tree_* fields aren't known to the Django query machinery and therefore cannot be used with .order_by() unfortunately. You have to go through the .extra() API. I added a test here which demonstrates a hopefully correct BFS ordering: https://github.com/matthiask/django-tree-queries/commit/1f144107ffef197d78083523c40f1a58ce0e8a91

atodorov commented 4 years ago

Thanks for the tip & the test. That works for me.