On 17 nov, 11:46, Fabio Natali <[EMAIL PROTECTED]> wrote:
> Hi everybody out there! :-)
>
> I have to cope with a few hundreds item tree/hierarchy in my Django
> project. Specifically, I'll have 500 hundreds leaves in a 3-levels
> tree with a dozen branches.
>
> I won't need much writing over this tree, I'll just need to read it
> and show data in a drop down menu.
>
> I've been told to use django-treebeard

That's what I was about to suggest !-)

> or django-mptt to speed up
> things, but I wrote down a first draft on my own:
>
> class Node(models.Model):
>     name = models.CharField(max_length=50)
>     parent = models.ForeignKey('self')
>
> class Product(models.Model):
>     name = models.CharField(max_length=50)
>     parent = models.ForeignKey(Node)
>     [some more info here]
>
> The point is, how can I create the root of my tree? Should I add some
> "blank=True, null=True" properties to my Node model? So to have:
>
> class Node(models.Model):
>     name = models.CharField(max_length=50)
>     parent = models.ForeignKey('self', blank=True, null=True)
>
> And then consider the "null-parented" node as the root one?

That's the canonical solution when using the adjacency list pattern,
yes.

> Does my code make any sense? Do you think it will turn out as too slow
> for my hierarchy?

If you are really confident that your hierarchy will never grow deeper
than three levels, the above solution might be good enough. But it's
still less efficient than the materialized path or the nested sets
patterns when it comes to reading a whole branch.

> Should I better rely on django-treebeard or
> django-mptt?

Well... I'm not sure there's any clearcut answer here. If you have
enough time, you may want to do a couple benchmarks on representative
data and use cases.

My 2 cents...
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to