Basically, it allows you to discover every children, or parents, or the whole tree, with a single query. Very efficient! And good for threaded discussions...

​Download the necessary files from my website. For now, it's just a model and a view (with a single function). I thought about adding a template tag but stick with the do-it-yourself flexibility.

In Django 0.96 the code given does not work.

The code in the update works with the svn trunk. Here's a ​good example of how to use this type of class. It also gives a good example of how to get the node when you have the content item it belongs to.

If you hack it to add some functionality, drop me an email (inerte is my gmail.com username). Current version is 0.9, and if it doesn't break anything, as soon as I finish editing the forms that I use to serve as an example, it will be 1.0. And if we depend on any new features, it will probably stay at this version forever :p

If you need more documentation, check old versions from this wiki page (specially the first and second), for a longer explanation of the whole thing, when mptt wasn't in a file for you to download, but just code that I pasted here.

How to install

Drop the mptt folder inside your Django project folder.

Edit the settings.py of your project and add a 'mptt' element into the INSTALLED_APPS array.

Change to the directory of your project (where manage.py is) and type:

python manage.py install mptt
python manage.py sqlindexes mptt

If you have to, copy the output of sqlindexes and apply to your database. This is very important for performance! The object_id and lft columns should be indexed!

Using it

Edit the views.py file from your app to call mptt's views.py and pass its function to a template:

Now we have a list called "node_tree" to use on your template. What's is it? There's a "stack" attribute on each node now that tells you "how far" each node is from the object. For example, every "root" node (the direct reply to a "post", for example) has a stack of 1. Every node that's an answer to the root nodes has a stack of 2. The stack respects the order (because of order_by=lft?) that the nodes were inserted at the database. So we end up with nodes having stacks numbered like this:

1
2
3
3
4
2
2
1
2
3
3

Template

If you want to show the nodes a little far from the left viewport border, based on their stack numbers, use this on your template:

I will leave to you to code the necessary forms to insert the new node on your public view. Just remember to give a new Node the object_id which we're replying to. If it's a Node itself, the new node/comment will act as a "reply". If it's for any other Django content, it's like a new "root" node/comment.

Other than that, the whole mptt model and view aren't very smart. Editing them using the admin interface is hard and doesn't make sense (because nodes represent threaded content, and not single objects).