Django Tree Libraries

django-mptt

  • Nested Set trees.
  • A register() call is used to set things up; it ads the necessary fields to the model.
  • A tree model still has a foreign key to itself. This is the API you use to manage the tree. Signals are used that the hidden tree fields are updated when the parent ForeignKey changes. No add_child() required.
  • Using the foreign key to self means that deletion is handled automatically be Django and/or the database. The other libraries need to implement a custom Queryset subclass to handle deletes.

django-treebeard

  • Has an awesome name.
  • In addition to the common Nested Set/MPTT approach, supports two other tree implementations. Materialized Path in particular is interesting.
  • You inherit your models from abstract base classes, which I like.
  • The tree has to be managed manually, that is, there are specific APIs like add_child() you have to call.
  • Unfortunately, those APIs are classmethods on the model rather than the Django-way, putting them into the model manager.

django-easy-tree

  • Apparently a fork of django-treebeard, but only supports Nested Set trees.
  • But has a prettier API that fits very well into Django: Nicer class names, properly puts methods into the manager when they belong there, options are specified inside “Meta” rather than on the model itself.
  • Has an interesting concept of validators. Included is a SingleRootAllowedValidator.
  • No tests!

Clearly, somebody needs to write a django-treebeard that uses the django-easy-tree API design and django-mptt’s signal approach.

2 thoughts on “Django Tree Libraries

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s