diff --git a/apps/assets/api/node.py b/apps/assets/api/node.py index 273ac667f..924dcb788 100644 --- a/apps/assets/api/node.py +++ b/apps/assets/api/node.py @@ -201,10 +201,8 @@ class NodeAddChildrenApi(generics.UpdateAPIView): def put(self, request, *args, **kwargs): instance = self.get_object() nodes_id = request.data.get("nodes") - children = [get_object_or_none(Node, id=pk) for pk in nodes_id] + children = Node.objects.filter(id__in=nodes_id) for node in children: - if not node: - continue node.parent = instance return Response("OK") diff --git a/apps/assets/models/node.py b/apps/assets/models/node.py index e88afccdc..da8d07ebc 100644 --- a/apps/assets/models/node.py +++ b/apps/assets/models/node.py @@ -103,7 +103,7 @@ class FamilyMixin: if value is None: value = child_key child = self.__class__.objects.create( - id=_id, key=child_key, value=value, parent_key=self.key, + id=_id, key=child_key, value=value ) return child diff --git a/apps/assets/signals_handler.py b/apps/assets/signals_handler.py index 1c2813aa9..da7f6d397 100644 --- a/apps/assets/signals_handler.py +++ b/apps/assets/signals_handler.py @@ -4,7 +4,7 @@ from operator import add, sub from assets.utils import is_asset_exists_in_node from django.db.models.signals import ( - post_save, m2m_changed, pre_delete, post_delete + post_save, m2m_changed, pre_delete, post_delete, pre_save ) from django.db.models import Q, F from django.dispatch import receiver @@ -37,6 +37,11 @@ def test_asset_conn_on_created(asset): test_asset_connectivity_util.delay([asset]) +@receiver(pre_save, sender=Node) +def on_node_pre_save(sender, instance: Node, **kwargs): + instance.parent_key = instance.compute_parent_key() + + @receiver(post_save, sender=Asset) @on_transaction_commit def on_asset_created_or_update(sender, instance=None, created=False, **kwargs):