Django - managing tree structures
I'm using Django with the following (simplified) models:
class Entity(models.Model):
name = models.CharField(max_length=30)
class Person(models.Model):
last_name = models.CharField(max_length=30)
first_name = models.CharField(max_length=30)
entity = models.ForeignKey(Entity)
I'd like to add hierarchy capabilities to the Entity model (and, by extension, to Person).
Question: which implementation do you recommend, given that I favor system stability and ease of maintenance over screaming-fast query speed (though unacceptable query speed across ~2000 leaf and node Entitys would be bad开发者_运维技巧).
1) Implementing the hierarchy tree as a nested-set within the SQL database, to be accessed via normal field operations; or
2) Implementing the hierarchy tree outside of Django as a regular Python tree (e.g. Node class), accessing hierarchy information via Django model methods?
Thanks,
Mike
I don't think your alternatives are really mutually exclusive - anything you end up doing will have some database and some non-database elements.
I'm a big fan of the Modified Pre-Order Tree Traversal algorithm, and its excellent Django implementation, django-mptt.
If you're dead set on Nested Sets, though, take a look at django-treebeard, which offers that, Adjacency List, and Materialized Path.
精彩评论