Looking at the mptt example http://code.djangoproject.com/wiki/ModifiedPreorderTreeTraversal it seemed a bit complicated for a normal user to understand. So i made my own, but considering i cannot group by makes aggregate functions hard to make this work nice, but magic-removal made it easier. Currently only works in postgesql and only tested in 8.1. Use at will and own RISK, but if/ when you improve it i would like to see it made available.
I attach the file and give a example here. Just add fields to this and it should work.
Remember to set the TABLE variable at the top of models to appname_model.
Example using project name myproject and app name pages.
from myproject.pages.models import Page tree = Page.manager.all() # get entire tree parent = Page.manager.get(pk=10) # get object you want to get the children of children = parent.get_children() # returns empty dict if no children are found parent = children[0].get_parent() # gets the parent
Attachments (2)
- models.py (10.0 KB ) - added by 19 years ago.
-
models.2.py
(10.0 KB
) - added by 19 years ago.
updated version
Download all attachments as: .zip