Skip to content

Commit b72e4cd

Browse files
committed
Fix reparenting code
1 parent 7bd8afd commit b72e4cd

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

django_pgtree/models.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,10 @@ def __init__(self, *args, parent=None, **kwargs):
3333
def parent(self):
3434
parent_path = self.tree_path[:-
3535
1] # pylint: disable=unsubscriptable-object
36-
print(parent_path)
3736
return self.__class__.objects.get(tree_path=parent_path)
3837

3938
@parent.setter
40-
def __set_parent(self, new_parent):
39+
def parent(self, new_parent):
4140
if new_parent.tree_path is None:
4241
raise ValueError(
4342
"Parent node must be saved before receiving children")
@@ -66,7 +65,7 @@ def save(self, *args, **kwargs): # pylint: disable=arguments-differ
6665
tree_path__descendant_of=self.__old_tree_path
6766
).update(
6867
tree_path=LtreeConcat(
69-
'.'.join(self.tree_path),
68+
models.Value('.'.join(self.tree_path)),
7069
Subpath(models.F('tree_path'), len(self.__old_tree_path)),
7170
)
7271
)

django_pgtree/tests.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,14 @@ def test_family(animal):
4141
mammal = T.objects.get(name='Mammal')
4242
assert {x.name for x in mammal.family} == {
4343
'Animal', 'Mammal', 'Cat', 'Dog'}
44+
45+
def test_reparent(animal):
46+
marsupial = T.objects.get(name='Marsupial')
47+
mammal = T.objects.get(name='Mammal')
48+
marsupial.parent = mammal
49+
marsupial.save()
50+
assert marsupial.tree_path[:2] == mammal.tree_path
51+
koala = T.objects.get(name='Koala')
52+
assert koala.parent == marsupial
53+
assert koala.tree_path[:2] == mammal.tree_path
54+
assert mammal in koala.ancestors

0 commit comments

Comments
 (0)