Django-mptt or treebeard for reddit-like comments?

I’m currently actively trying to figure out which of django-mptt or django-treebeard can best handle being applied for reddit-like comments. Specifically I am talking about the Materialized Path trees in django-treebeard vs django-mptt.

The key question for me is which library is best able to handle things like e.g. sorting comments by ‘Newest’, ‘Oldest’, ‘Highest Vote Score’, etc.

For the Materialized Path implemntation with django-treebeard, because the path is set upon insert, it seems really difficult to be able to dynamically resort the comments according to their vote score (which is liable to change sporadically). Is this even possible with django treebeard?

I’m really trying to make django-treebeard work because using Materialized Paths is supposed to be the better, faster option (django-cms and wagtail both moved to django-treebeard), but I just have no idea how to implement dynamically sorted comments with it or if it’s even possible.

Currently the materialized path that is built corresponds to being sorted by newest. But how is it possible to create a path sorted by score, if the path is determined on insert?

So maybe to rephrase my question, Are the osteniible benefits of Materialized Paths (fast reads, fastish writes) worth the monkeywrenching that’s required with django-treebeard (for the most part because of spurious documentation for this particular use case [good documentation otherwise]) vs just using full-batteried but probably slower django-mptt?

The main problem with django treebeard is it denormalizes the relationship between parent and child, so resorting the tree is an extra headache.

Think of the materialized path as your primary key. Its sort order really is irrelevant to what you’re trying to accomplish. (It’s the same with the typical integer primary key of a regular table - you don’t pay any attention to how its sequenced.)

Your sorting should be an issue of how the data is retrieved and prepared for display, not of how its physically stored in the database. (This is especially true if you’re going to provide the facility for allowing someone to select different sort criteria.)

1 Like

Hey Ken, thanks for your reply!

That actually really helps to clarify my thinking, thank you. Thinking of the materialized path as the primary key is a great tip. I’ll focus my efforts on sorting the tree during display then, that makes a lot of sense.

Also, if anyone comes here after me with the same questions, I found this great blog article that tackles a similar problem with a relevant solution for recursive template logic for django treebeard: https://elfsternberg.com/2009/05/18/template-recursion-in-django-with-treebeard/.

Thanks again Ken! I see you’ve answered a lot of questions on this forum, thanks for all the good work & help eh! It’s definitely appreciated. Hope you’re doing well!