Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrong results when use `get_ancestors` functions from `django-mptt`

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.

like image 591
idea.list Avatar asked Sep 29 '12 16:09

idea.list


2 Answers

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()
like image 184
Serhii Holinei Avatar answered Nov 10 '22 02:11

Serhii Holinei


agree with Sergey's answer. The command I use to rebuild the tree is:

ModelName.objects.rebuild()   

(executed in 'python manage.py shell')

like image 1
burney Avatar answered Nov 10 '22 00:11

burney