From cbc4a0a97b4f0a213f8d096a0bd1347c6d9475b6 Mon Sep 17 00:00:00 2001 From: BaiJiangjie Date: Fri, 11 May 2018 11:27:48 +0800 Subject: [PATCH] =?UTF-8?q?[Bugfix]=20=E8=B5=84=E4=BA=A7=E6=A0=91=E8=8A=82?= =?UTF-8?q?=E7=82=B9Node=20Value=E5=80=BC=E6=A0=A1=E9=AA=8C=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E5=BC=82=E5=B8=B8=E6=8A=9B=E5=87=BA=E4=B8=BA400?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/assets/api/node.py | 5 ++++- apps/assets/serializers/node.py | 11 +++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/apps/assets/api/node.py b/apps/assets/api/node.py index 2ea2d112c..b8b2707e6 100644 --- a/apps/assets/api/node.py +++ b/apps/assets/api/node.py @@ -14,6 +14,7 @@ # limitations under the License. from rest_framework import generics, mixins +from rest_framework.serializers import ValidationError from rest_framework.views import APIView from rest_framework.response import Response from rest_framework_bulk import BulkModelViewSet @@ -110,7 +111,9 @@ class NodeChildrenApi(mixins.ListModelMixin, generics.CreateAPIView): value = request.data.get("value") values = [child.value for child in instance.get_children()] if value in values: - raise AssertionError('The same level node name cannot be the same') + raise ValidationError( + 'The same level node name cannot be the same' + ) node = instance.create_child(value=value) return Response( {"id": node.id, "key": node.key, "value": node.value}, diff --git a/apps/assets/serializers/node.py b/apps/assets/serializers/node.py index 9f04fd465..bee350591 100644 --- a/apps/assets/serializers/node.py +++ b/apps/assets/serializers/node.py @@ -51,14 +51,17 @@ class NodeSerializer(serializers.ModelSerializer): fields = ['id', 'key', 'value', 'parent', 'assets_amount', 'is_node'] list_serializer_class = BulkListSerializer - def update(self, instance, validated_data): - value = validated_data.get('value') + def validate(self, data): + value = data.get('value') + instance = self.instance if not instance.is_root(): children = instance.parent.get_children().exclude(key=instance.key) values = [child.value for child in children] if value in values: - raise AssertionError('The same level node name cannot be the same') - return super().update(instance, validated_data) + raise serializers.ValidationError( + 'The same level node name cannot be the same' + ) + return data @staticmethod def get_parent(obj):