从`django-mptt`使用`get_ancestors`函数时出现错误的结果

ide*_*ist 5 python django tree hierarchy django-mptt

我正在开发一个使用django-mptt的项目,但是当我使用get_ancestors函数时,我得到了奇怪的结果.这是一个例子.
我有一个创建了一个简单的模型,继承自MPTTModel:

class Classifier(MPTTModel):
    title = models.CharField(max_length=255)
    parent = TreeForeignKey('self', null = True, blank = True, 
                            related_name = 'children')

    def __unicode__(self):
        return self.title
Run Code Online (Sandbox Code Playgroud)

以下是适用于此模型的功能:

def test_mptt(self):
    # Erase all data from table
    Classifier.objects.all().delete()

    # Create a tree root
    root, created = Classifier.objects.get_or_create(title=u'root', parent=None)

    # Create 'a' and 'b' nodes whose parent is 'root'
    a = Classifier(title = "a")
    a.insert_at(root, save = True)
    b = Classifier(title = "b")
    b.insert_at(root, save = True)

    # Create 'aa' and 'bb' nodes whose parents are
    # 'a' and 'b' respectively
    aa = Classifier(title = "aa")
    aa.insert_at(a, save = True)
    bb = Classifier(title = "bb")
    bb.insert_at(b, save = True)

    # Create two more nodes whose parents are 'aa' and 'bb' respectively
    aaa = Classifier(title = "aaa")
    aaa.insert_at(aa, save = True)
    bba = Classifier(title = "bbb")
    bba.insert_at(bb, save = True)

    # Select from table just created nodes
    first = Classifier.objects.get(title = "aaa")
    second = Classifier.objects.get(title = "bbb")

    # Print lists of selected nodes' ancestors:
    print first.get_ancestors(ascending=True, include_self=True)
    print second.get_ancestors(ascending=True, include_self=True)
Run Code Online (Sandbox Code Playgroud)

我希望在输出上看到下一个值:

[<Classifier: aaa>, <Classifier: aa>, <Classifier: a>, <Classifier: root>]
[<Classifier: bbb>, <Classifier: bb>, <Classifier: b>, <Classifier: root>]
Run Code Online (Sandbox Code Playgroud)

但是我看到了:

[<Classifier: aaa>, <Classifier: bb>, <Classifier: b>, <Classifier: root>]
[<Classifier: bbb>, <Classifier: bb>, <Classifier: b>, <Classifier: root>]
Run Code Online (Sandbox Code Playgroud)

因此,当您看到此函数打印bbb节点的正确祖先列表,但节点的错误祖先aaa.你能解释一下为什么会这样吗?这是一个错误django-mptt或我的代码是不正确的?

提前致谢.

Ser*_*ney 5

当您将节点插入到树中时,它会导致整个树发生变化。因此,当您插入b节点时,您的aroot节点在数据库中会发生变化,但您的变量不会更新并保留为包含旧的左/右值,这些值用于构建正确的树结构。

在您的情况下,当 lineaa.insert_at(a, save = True)在 proccess 中时,您的a变量包含一个具有lft= 2 和rght= 3的旧实例,而在数据库a节点中包含lft= 4 和rght= 5。

在插入新项目之前,您需要获取父项的新实例。最简单的方法是运行refresh_from_db

aa.refresh_from_db()
Run Code Online (Sandbox Code Playgroud)