3

我用的是 django-mptt 版本 (0,5,'+dev')

我的模型看起来像:

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']

现在,我在 Comment 模型中更改 Meta:

class MPTTMeta:
        order_insertion_by = ['creation_time']

然后,我在 django shell 下重建树,然后是这个

模型.comment.tree.rebuild()

但是,它抛出: AttributeError: type object 'Comment' has no attribute 'tree'

那有什么问题?如何在 django-mptt 中重建树?

谢谢!

4

1 回答 1

4

你有没有尝试过:

Comment.objects.rebuild()

因为rebuild是在_TreeManager class

在您引用的 SO 文章中,我假设他已为该tree属性设置了一个自定义管理器。但你没有,因此在objects属性上。

您熟悉模型管理器吗?

于 2012-09-08T18:24:16.653 回答