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

Filter by depth #45

Closed jmbarbier closed 2 years ago

jmbarbier commented 2 years ago

This is more a help request than a bug...

Transitioning from django-mptt, all my tree needs are so far OK using django-tree-queries, except one : i work on pretty deep trees, and i need to filter on max depth. filter(tree_depth__lte=4) is not possible with django-tree-queries.

I use for now a filter(Q(parent__isnull=True) | Q(parent__parent__isnull=True) | (...)__isnull=True) but this is a bit ugly. I could also store the depth into model, but this is a step back from django-tree-queries

What would be the more django-tree-queries-way to do this ?

Thanks

matthiask commented 2 years ago

Hi @jmbarbier

The documentation is still very sparse so I'm afraid that the test suite is still the best place to document these things; I added a test for filtering by depth, see the referenced commit above or here:

https://github.com/matthiask/django-tree-queries/blob/1ea2233ba1601c51757bbd90f508110207fad9cc/tests/testapp/test_queries.py#L525-L542

Thanks!