On Mon, Jul 14, 2008 at 6:27 AM, MarC <[EMAIL PROTECTED]> wrote:
>
> Hello everybody,
>
> I'm trying to come up with the most django simple friendly way to store
> records in a "tree" kind of database. For instance my records are
...
> # Create your models here.
> class Food(models.Model):
>  name = models.CharField(max_length=50);
>  parents = models.ForeignKey('Food');
>  class Admin:
>        pass
>  >>
>
> But I'm having trouble to create a root "Food" node with no ForeignKey
> defined or ForeignKey pointed to himself from the admin site.

Firstly, if you want to take this apprach, you identify a root node by
setting parents=None. In order to do this, you need to modify your
model to allow the parents field to accept null values (i.e.,
null=True as a setting on the field).

Secondly, while this approach is the easiest way to represent a tree,
it isn't the easiest structure to query. You need to read up on MPTT
(Modified Preorder Tree Traversal). The django-mptt project [1] is
probably also worth looking into - if only because it has some great
links on representing trees in relation databases.

[1] http://code.google.com/p/django-mptt/

Yours,
Russ Magee %-)

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