generic ModifiedPreorderTreeTraversal app

Skip to first unread message


Jul 27, 2006, 12:05:28 AM7/27/06
hi all,

Base upon Julio's mptt-0.9 which handles nested contents:

I made this more "generic" version mptt-0.95, from the README:

orignal mptt readme

Follow up by me


First of all, ***BIG Thanks*** to `Gijs Van Tulder's excellent
article`_ on the MPTT subject &

Julio's GREAT work on this django implementation!

mptt-0.95 (following django's svn version number:) based on Julio's
code, and make it even more


- Julio's version treats the root content (for example, an Article
object which is going to be

commented/follow-up'd) & the tree of the root content differently),
so the Node model is in

response for both "describing a tree" & "storing node content".

- In this modified version, I "limit" the Node model's responsibility
to only "describing a

tree": you keep storing your model's data in a "flat" way, once
necessary, you can organize

them into a "tree", and, ***you can organize this tree across all of
the models of your site***,

that means, you can build your tree with User, Article, Group,
WhatEver model's object instances

as the tree's node:)

While comparing the code between Julio's original version and my
modified version, please note:

Both versions are using the GenericForeignKey_ like technique, and
both have content_type &

object_id fields, however, they are different:

- Julio's version uses these fields to refer to the ***root content
object of the Node tree***;

- This modified version uses them to refer to ***content object that
becomes a nodes of the tree***

Please check the code for detail:)

Sample code


from import Article

from agv.apps.mptt.models import Node

a1 = Article.objects.get(pk=1)

a2 = Article.objects.get(pk=2)

a3 = Article.objects.get(pk=3)

a4 = Article.objects.get(pk=4)

a5 = Article.objects.get(pk=5)

n1 = Node(content_object=a1) # the root node

# 2nd level nodes

n2 = Node(content_object=a2, parent=a1)

n3 = Node(content_object=a3, parent=a1)

# 3rd level nodes

n4 = Node(content_object=a4, parent=a2)

n5 = Node(content_object=a5, parent=a3)

# you can even do this

from import Banana

b = Banana.objects.get(pk=1)

# add the banana to the tree of articles! :-p

n6 = Node(content_object=b, parent=a1)

Thanks again to Julio's code!

Please help me to test it if you're interested:)

Sorry for my poor English! With any problem, find me at:

Eric Hsu

.. _Gijs Van Tulder's excellent article:

.. _GenericForeignKey:



Cheng Zhang

Jul 27, 2006, 12:25:48 AM7/27/06
If you get time, I suggest you update the wiki page accordingly.

- Cheng Zhang

> > <mptt-0.95.tar.bz2>


Jul 27, 2006, 5:16:21 AM7/27/06
to Django users
Cheng Zhang wrote:
> Great.
> If you get time, I suggest you update the wiki page accordingly.

ok, I'll do that soon.

plus, a bug fix to the

in the node_tree method, the node_list line should add an
order_by('lft') to it:

node_list = Node.objects.filter(tree_id__exact=root.tree_id,
lft__range=(root.lft, root.rght)).order_by('lft')

otherwise the tree doesn't diplay correctly :p

Reply all
Reply to author
0 new messages