How to get move() operation to work in Django Treebeard

80 views Asked by At

I'm having trouble getting the move() operation to work in Django Treebeard. Here's my model:

class TestNode(MP_Node):
    name = models.CharField(max_length=200)

    node_order_by = ['name']

And a test:

class TestNodeTestCase(TestCase):

    def test_move_test_node(self):
        get = lambda node_id: TestNode.objects.get(pk=node_id)

        # create ROOT -> parent -> child tree
        root = TestNode.add_root(name='ROOT')
        parent = get(root.id).add_child(name='parent')
        child = get(parent.id).add_child(name='child')

        # just to be on the safe side, requery all nodes
        root = get(root.id)
        parent = get(parent.id)
        child = get(child.id)

        # tree looks good:
        # [{'data': {'name': 'ROOT'}, 'id': 1, 'children': [{'data': {'name': 'parent'}, 'id': 2, 'children': [{'data': {'name': 'child'}, 'id': 3}]}]}]
        print(TestNode.dump_bulk())

        # now move the child from the parent to ROOT
        child.move(root, 'sorted-child')
        child.save()

        # If I try dumping the tree here, I get this exception:
        # KeyError: '00010001'
        # print(TestNode.dump_bulk())

        # requery all nodes
        root = get(root.id)
        parent = get(parent.id)
        child = get(child.id)

        # child's new parent should be ROOT
        self.assertEqual(child.get_parent().id, root.id)

I get:

myproj.models.TestNode.DoesNotExist: TestNode matching query does not exist

I have read that most of the operations are performed in SQL, so I am obsessively reloading the model instances, but it's still not working. What am I doing wrong here? Thanks!

0

There are 0 answers