I’m using django-mptt 0.4.2, and want to rebuild a tree.
The tree manager has a method rebuild() which I try to access like this:
>>> my_rootnode = MyObj.objects.get(id=12)
>>> my_rootnode.tree.rebuild()
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/local/lib/python2.6/dist-packages/django/db/models/manager.py", line 211, in __get__
raise AttributeError("Manager isn't accessible via %s instances" % type.__name__)
AttributeError: Manager isn't accessible via MyObj instances
I’m obviously doing this wrong. How should I access the rebuild method?
mptt Managerinherits fromdjango.db.models.Managerwhich can not be accessed via model instances but only via model classes. More infos:Retrieving objectsThe model class here is
MyObj. You are using a model instancemy_rootnodethe correct usage is:
MyObj.tree.rebuild()(documentation link)this will build MyObj tree.