Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django-mptt tree rebuild error

I used django-mptt version (0,5,'+dev')

My model looks like:

class Comment(MPTTModel):
    content = models.CharField(max_length = 300)
    parent = TreeForeignKey('self', null=True, blank=True, related_name='child')

    class MPTTMeta:
        order_insertion_by = ['-creation_time']

Now, I change Meta in Comment model:

class MPTTMeta:
        order_insertion_by = ['creation_time']

then, I rebuild the tree under the django shell followed by THIS:

models.comment.tree.rebuild()

However, it throws: AttributeError: type object 'Comment' has no attribute 'tree'

What's wrong with that? How to rebuild the tree in django-mptt?

Thanks!

like image 227
hushwings Avatar asked Dec 12 '25 12:12

hushwings


1 Answers

Have you tried:

Comment.objects.rebuild()

Because rebuild is a function defined on the TreeManager class

In the SO article you referenced, I assume he had set a custom manager to the tree attribute. But you haven't and thus is on the objects attribute.

Are you acquainted with Model Managers?

like image 93
Andre Bossard Avatar answered Dec 15 '25 11:12

Andre Bossard



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!