-
-
Notifications
You must be signed in to change notification settings - Fork 27
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
Limit number of results per depth #70
Comments
You can do something like this by introducing your own Of course this doesn't check anything on the database level, but maybe it's good enough? def clean(self):
if not self.parent_id and Node.objects.filter(Q(parent=None) & ~Q(id=self.id)).count() > 10:
raise ValidationError(...)
if self.parent_id and self.parent.children.filter(~Q(id=self.id)).count() > 2:
raise ValidationError(...) You could maybe also do something using a database constraint, but you'd have to research this yourself for now. |
Thank you for responding so quickly c: What i want is to make a depth first QuerySet that can be paginated, ordered by score, and allows only 10 childrens, and 2 grandchildrens before jumping to next parent node Although i'm not sure how to explain this, but i'll try! Below is an example of what i want the queryset to return (in ascending order)
├── 1/ # Root node begins here |
Ah, yes.
Pagination is always much harder than it looks at first; easier ways exist maybe per use case, e.g. you could only paginate the root nodes and just accept what descendants you get or whatever. I'm not sure I understand your requirements completely, but maybe the ideas here will help you with the implementation! It certainly sounds doable with django-tree-queries. |
You're getting closer that's for sure c: However, i forgot to mention a few things again, i'll try more! The descendants that i show with the "..." is not a limitation, the model will allow these nodes to exist, which is why a clean function wouldn't work for this! Also, the root node as described can also be a child, or a grand child and so-on, the QuerySet can start from anywhere on this Tree and give the same structure back, even if it starts on a Node which is at 10, 20, or even a hundred depth Thanks again for your blazing fast responses!! |
The problem is then that tree queries will always build a tree of at least all descendants of the current node; you can apply a limit like (untested!) django-tree-queries/tree_queries/query.py Lines 53 to 77 in ec23dc1
I don't really want to point anyone towards some of the less well maintained alternatives like django-mptt, but it sounds as if something like https://www.postgresql.org/docs/current/ltree.html or maybe some other tree/graph database could be a better fit. I cannot vouch for any Django / ltree integration packages since I have never used one of them. The trees I encounter most of the time are at most a few hundred nodes large so it's no question that the recursive CTE approach is the right one. For (much) larger trees I don't know, but Disqus has certainly also had much success using recursive CTEs for their threaded comments. Btw, if you know how to do what you want in raw SQL, don't fear dropping down to it if the ORM isn't expressive enough or if there are no libraries around. If you don't know how to do it in raw SQL, maybe first try researching that just to get a feel for the problem and maybe discover ways to slightly alter the requirements to make the implementation more straightforward.
No problem, it's certainly an interesting problem! |
The new django-tree-queries/tree_queries/compiler.py Lines 77 to 91 in ec23dc1
To exclude ancestors you will need to specify in the SQL which nodes you want to be treated as root nodes. The simplest way to do this within the framework of tree-queries is to change the above WHERE statement to filter on a set of primary keys rather than if a node has a null parent. You could then apply something like .extra(where=["tree_depth <= 2"]) to remove any children based on depth.
|
I'd like to use this library, but i just wonder if it's possible to set a limit of e.g. max 10 childrens per root node and two children per child node?
The text was updated successfully, but these errors were encountered: