我正在开发一个使用django-mptt的项目,但是当我使用get_ancestors函数时,我得到了奇怪的结果.这是一个例子.@H_404_2@我有一个创建了一个简单的模型,继承自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
以下是适用于此模型的功能:
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)
我希望在输出上看到下一个值:
[
但是我看到了:
[
因此,当您看到此函数为bbb节点打印正确的祖先列表,但aaa节点的错误祖先.你能解释一下为什么会这样吗?这是django-mptt中的错误还是我的代码不正确?
提前致谢.
最佳答案