Base upon Julio's mptt-0.9 which handles nested contents:
http://code.djangoproject.com/wiki/ModifiedPreorderTreeTraversal
I made this more "generic" version mptt-0.95, from the README:
[snip]
orignal mptt readme
[/snip]
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
"generic":
- 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 path.to.your.models 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
n1.save()
# 2nd level nodes
n2 = Node(content_object=a2, parent=a1)
n2.save()
n3 = Node(content_object=a3, parent=a1)
n3.save()
# 3rd level nodes
n4 = Node(content_object=a4, parent=a2)
n4.save()
n5 = Node(content_object=a5, parent=a3)
n5.save()
# you can even do this
from path.to.your.other.models import Banana
b = Banana.objects.get(pk=1)
# add the banana to the tree of articles! :-p
n6 = Node(content_object=b, parent=a1)
n6.save()
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:
http://www.sitepoint.com/print/hierarchical-data-database
.. _GenericForeignKey:
http://feh.holsman.net/articles/2006/06/19/django-generic-relations
--
- http://www.ifaxian.com
- http://groups.google.com/group/nkeric-daily
- http://nkeric.3322.org
- Cheng Zhang
> > <mptt-0.95.tar.bz2>
ok, I'll do that soon.
plus, a bug fix to the views.py
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