generic ModifiedPreorderTreeTraversal app
flag
3 messages - Collapse all
/groups/adfetch?adid=qI3dphAAAADRqRNo5R0CG0xuDz1A2jE9
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
1.  nkeric  
View profile  
(1 user)  More options Jul 27 2006, 12:05 am
From: nkeric <nke...@gmail.com>
Date: Thu, 27 Jul 2006 12:05:28 +0800
Local: Thurs, Jul 27 2006 12:05 am
Subject: generic ModifiedPreorderTreeTraversal app

hi all,

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

nke...@gmail.com

.. _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

  mptt-0.95.tar.bz2
4K Download

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
2.  Cheng Zhang  
View profile  
 More options Jul 27 2006, 12:25 am
From: Cheng Zhang <czhang....@gmail.com>
Date: Thu, 27 Jul 2006 12:25:48 +0800
Local: Thurs, Jul 27 2006 12:25 am
Subject: Re: generic ModifiedPreorderTreeTraversal app
Great.
If you get time, I suggest you update the wiki page accordingly.

- Cheng Zhang

On Jul 27, 2006, at 12:05 PM, nkeric wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
3.  nkeric  
View profile  
 More options Jul 27 2006, 5:16 am
From: "nkeric" <nke...@gmail.com>
Date: Thu, 27 Jul 2006 09:16:21 -0000
Local: Thurs, Jul 27 2006 5:16 am
Subject: Re: generic ModifiedPreorderTreeTraversal app

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 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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google