I'm developing a project which uses django-mptt, but I get strange results when I use get_ancestors
function. Here is an example.
I have a created a simple model, inherited from 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
And here is the function which works with this model:
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)
I expected to see next values on the output:
[<Classifier: aaa>, <Classifier: aa>, <Classifier: a>, <Classifier: root>]
[<Classifier: bbb>, <Classifier: bb>, <Classifier: b>, <Classifier: root>]
But insted I see:
[<Classifier: aaa>, <Classifier: bb>, <Classifier: b>, <Classifier: root>]
[<Classifier: bbb>, <Classifier: bb>, <Classifier: b>, <Classifier: root>]
So as you see this function prints correct list of ancestors for bbb
node, but wrong ancestors for aaa
node. Can you explain me why this happens? Is this a bug in django-mptt
or my code is incorrect?
Thanks in advance.
When you insert a node into a tree it causes a changes in a whole tree. So when you insert b
node, your a
and root
nodes change at database, but your variables doesn't get updated and remain to contain old left/right values, which are used to build correct tree structure.
In your case, when line aa.insert_at(a, save = True)
is in proccess, your a
variable contains an old instance with lft
= 2 and rght
= 3, while in database a
node contains lft
= 4 and rght
= 5.
You need to get fresh instance of a parent before inserting a new item. The easiest way to do this is to run refresh_from_db
:
aa.refresh_from_db()
agree with Sergey's answer. The command I use to rebuild the tree is:
ModelName.objects.rebuild()
(executed in 'python manage.py shell')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With