Multiple trees per node

96 views
Skip to first unread message

Daniel Naab

unread,
Oct 29, 2011, 2:08:31 PM10/29/11
to django-mptt-dev
Hi Folks,

I've been considering branching django-mptt to support the notion of
multiple trees per node, essentially implementing a more complex
directed graph but maintaining the tree abstraction. Here's the kind
of situation I'm thinking about; does it make sense?

class Node(MPTTModel):
parent = TreeForeignKey('self', null=True, blank=True,
related_name='children')
parent_revision = TreeForeignKey('self', prefix='revision',
null=True, blank=True, related_name='child_revisions')

Essentially, the "prefix" kwarg to TreeForeignKey would instruct the
metaclass to contribute "revision_lft", "revision_rght", etc, fields
to the class instead of "lft", "rght", and also new accessors like
"get_revision_descendent_count()". As you can discern, my use case
involves tracking revision history for nodes in a tree.

This might be opening up a can of worms, when simply having a foreign
key on Node to another tree achieves the same goal. However, this can
be expensive on large trees if you need the second parent reference on
each node. In addition, these really are two trees (although they
could be considered more complex graphs), so the abstraction is nice
and tidy.

Any thoughts or suggestions? Bad idea, or is there another efficient
way to handle this case?

Thanks,
Daniel

Craig de Stigter

unread,
Oct 30, 2011, 3:56:56 PM10/30/11
to django-...@googlegroups.com
I can't think of a better solution. That doesn't mean I think it's a good solution though ;) 

Also since there'll be a lot of fundamental changes and it's a bit of an unusual use-case, I can't imagine it'll be mergeable to core anytime soon, or maybe ever. I'd be open to it but we'd have to see once the implementation details are sorted out.

Craig de Stigter

Daniel Naab

unread,
Oct 30, 2011, 10:05:41 PM10/30/11
to django-mptt-dev
I've definitely gathered, after spending some time browsing the code,
that it wouldn't be trivial to make these changes. The largest issue
seems to be maintaining backward-compatibility without making the code
too ugly. However, this evening I decided to give it a try and see
what comes of it. I'll spend some more time on it evenings this next
week.

https://github.com/danielnaab/django-mptt

If you have any suggestions, please send them my way.

Thanks,
Daniel

KK

unread,
Apr 25, 2017, 3:57:23 PM4/25/17
to django-mptt-dev, danie...@gmail.com
I have a very similar use case where I need to support multiple hierarchy where a node may participate at a different level. Is there any code in a repo I can use as a starting point. If not I will start extending. 
Thanks 
KK

KK

unread,
Oct 4, 2017, 11:58:36 PM10/4/17
to django-mptt-dev
Finally the use case for multiple hierarchy came up for my product. I started updating the MPTT library but then found a different way to solve the problem. 
The idea is to create different model for each hierarchy we want and provide a FK relationship to the main node. 

class Node(model.Model): 
       name = model.CharField(max_length=200)

class Hierarchy1Node(MPTTModel):
    node = models.ForeignKey(Node)
    parent = TreeForeignKey('self', null=True, blank=True, related_name='children') 

class Hierarchy2Node(MPTTModel):
    node = models.ForeignKey(Node)
    parent = TreeForeignKey('self', null=True, blank=True, related_name='children') 

This keeps the hierarchy representation separate from the data itself. 
This trick works for my use case and could be useful to others too. 

-KK
Reply all
Reply to author
Forward
0 new messages