What are the advantages of django-treebeard over django-mptt?
Asked Answered
K

3

15

There are two well known Django packages for creating tree structures: django-treebeard and django-mptt. Recently Django CMS started using django-treebeard instead of django-mptt. Wagtail CMS is also using django-treebeard.

What makes django-treebeard a more preferable choice than django-mptt?

Kutchins answered 20/10, 2015 at 11:58 Comment(1)
probably a duplicate of #1682818Marashio
M
16

The main difference is choice of SQL tree implementation.

django-mptt uses nested sets, which is fast for reads, slow for writes.

Treebeard offers nested sets, as well as adjacency lists (fast writes, slow reads) and materialized path (fast reads, fastish writes).

There are also other differences. django-mptt has a nicer API and better docs.

Mozzarella answered 22/5, 2016 at 13:31 Comment(0)
A
5

Most importantly, django-mptt is explicitly unmaintained:

https://github.com/django-mptt/django-mptt#this-project-is-currently-unmaintained

So, at this point, using django-mptt carries the significant risk that you won't be able to use current versions of other packages (depended on by django-mptt) and will eventually run into problems that will require you to switch to a different package like django-treebeard.

side note for Postgres users

Incidentally, for Postgres users, there is another interesting alternative to mptt: ltree (https://www.postgresql.org/docs/current/ltree.html).

Currently (at the time I'm writing this), there's no maintained package for integrating ltree with Django. However, django-treebeard has an open issue for adding ltree support: https://github.com/django-treebeard/django-treebeard/issues/170

In the meantime, it's easy enough to implement without a package. Here's a demo I found: https://github.com/peopledoc/django-ltree-demo. I can confirm that this demo still works well (even though the code is a few years old).

Aniseed answered 14/5, 2021 at 21:11 Comment(0)
I
2

You get more options with treebeard, which allows several tree implementations with the same API.

TREEBEARD: Adjacency List, Materialized Path, & Nested Sets

MPTT: NESTED sets

Also, MPTT may be slower in big-tree-operations involving more than 1000 nodes, so I'd say project size should factor into your considerations.

For more information checkout this DjangoCon Talk by one of the TREEBEARD maintainers Jacob Rief. The talk is on "Representing Hierarchies in Relational Databases" Check out his GitHub

Instinct answered 13/7, 2020 at 11:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.