[Update] 创建子节点支持id

pull/2402/head
ibuler 2019-02-18 12:59:36 +08:00
parent 49404f763d
commit c4af6fa72d
2 changed files with 4 additions and 3 deletions

View File

@ -163,12 +163,13 @@ class NodeChildrenApi(mixins.ListModelMixin, generics.CreateAPIView):
def create(self, request, *args, **kwargs):
instance = self.get_object()
value = request.data.get("value")
_id = request.data.get('id') or None
values = [child.value for child in instance.get_children()]
if value in values:
raise ValidationError(
'The same level node name cannot be the same'
)
node = instance.create_child(value=value)
node = instance.create_child(value=value, _id=_id)
return Response(self.serializer_class(instance=node).data, status=201)
def get_object(self):

View File

@ -134,10 +134,10 @@ class Node(OrgModelMixin):
count = max(values) + 1 if values else 1
return '{} {}'.format(name, count)
def create_child(self, value):
def create_child(self, value, _id=None):
with transaction.atomic():
child_key = self.get_next_child_key()
child = self.__class__.objects.create(key=child_key, value=value)
child = self.__class__.objects.create(id=_id, key=child_key, value=value)
return child
def get_children(self, with_self=False):