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

Document .tree_fields() (was: Providing queryset in `ancestors`) #76

Closed saveshodhan closed 2 months ago

saveshodhan commented 3 months ago

Hello,

I have a usecase in which I fetch multiple leaf-nodes, and each of them need to display their ancestors (including self). There is this method - ancestors that does this, but if we have multiple nodes then this results into those many hits to the DB.

I am thinking, if we make a provision to accept a queryset in this method, the method can just use it rather than hitting the db each time. Wdyt?

Currently i implemented a custom method that does the same thing, but it would be really cool to have this baked into the library itself.

If you think this is a good idea I can also take it up and submit a PR..

Thanks!

matthiask commented 3 months ago

Hi,

I'm not 100% sure what you're proposing, can you show an example?

Also, have you seen the .tree_fields() function tested here? https://github.com/feincms/django-tree-queries/blob/9b3af54bfe4ea0d25db776f9ce9c9d48f88a96ce/tests/testapp/test_queries.py#L994-L1014 Maybe this already does what you need?

If not, I'm certainly interested in PRs as long as they do not expand the API and maintenance headaches too much :) Thanks!

saveshodhan commented 3 months ago

ah you are right! tree_names worked for me!! :pray:

matthiask commented 3 months ago

Nice! I'll keep this open because tree_fields() should be mentioned somewhere in the README.