django-tree-queries icon indicating copy to clipboard operation
django-tree-queries copied to clipboard

`select_related`, out of scope?

Open SafaAlfulaij opened this issue 4 years ago • 3 comments

I was wondering what features will be considered out of scope and too much for this library, and what features isn't. For example, in the test suite, how can we query ReferenceModel and select the tree model with it's tree fields?

SafaAlfulaij avatar May 16 '21 05:05 SafaAlfulaij

It's hard to say. It depends on how complex it will be. I really want to keep the API and the implementation small and simple. If fear that adding the CTE to a query where the tree model isn't the base model of the queryset will complicate things, maybe a lot and introduce tricky edge cases which will have to be fixed when new Django versions are released. The beauty of django-tree-queries is that it mostly works (or worked) the same for Django 1.8 up to 3.2.

(The ReferenceModel case could also be handled by querying the tree model and prefetching the reference model instances, maybe using a Prefetch() instance. I know that this is just an example – I'm writing this down just as an illustration of my thinking in this case.)

matthiask avatar May 16 '21 08:05 matthiask

It's hard to say. It depends on how complex it will be. I really want to keep the API and the implementation small and simple. If fear that adding the CTE to a query where the tree model isn't the base model of the queryset will complicate things, maybe a lot and introduce tricky edge cases which will have to be fixed when new Django versions are released. The beauty of django-tree-queries is that it mostly works (or worked) the same for Django 1.8 up to 3.2.

I tried it, and most of the new code is copied from TreeCompiler.as_sql, with modified extra (to not sort or filter), and a custom join to the CTE table. Still, it is complicating stuff, and can break if Django update their internals, and doesn't support nested relations, and will brake with some edge cases. I'll continue checking and see how it fits.

(The ReferenceModel case could also be handled by querying the tree model and prefetching the reference model instances, maybe using a Prefetch() instance. I know that this is just an example – I'm writing this down just as an illustration of my thinking in this case.)

This actually might be the proper solution (although it does another query), since it adds the fields to the actual tree model, rather than adding them to the reference-by model (how annotate works generally).

SafaAlfulaij avatar May 16 '21 10:05 SafaAlfulaij

We have had a few issues with subqueries, annotations etc. not working properly in the last three years; not many, but too many to think that something like supporting select_related would be simple enough to be achievable without complicating everything too much.

So, I think that this feature request is out of scope, and we should instead add documentation on how to work within the current constraints.

matthiask avatar Apr 25 '24 17:04 matthiask