From 363baece4fc5d853fd4bc414d27b87346cb760e4 Mon Sep 17 00:00:00 2001 From: Bai Date: Wed, 11 Aug 2021 11:18:18 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E8=BF=9C=E7=A8=8B=E5=BA=94=E7=94=A8=E5=85=B3=E8=81=94=E8=B5=84?= =?UTF-8?q?=E4=BA=A7=E5=90=8E=EF=BC=8C=E6=9B=B4=E6=96=B0=E9=A1=B5=E9=9D=A2?= =?UTF-8?q?=E6=98=BE=E7=A4=BA=E8=B5=84=E4=BA=A7ID=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../attrs/application_category/remote_app.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/apps/applications/serializers/attrs/application_category/remote_app.py b/apps/applications/serializers/attrs/application_category/remote_app.py index 3cda4ac1e..fd5fa54f3 100644 --- a/apps/applications/serializers/attrs/application_category/remote_app.py +++ b/apps/applications/serializers/attrs/application_category/remote_app.py @@ -5,7 +5,7 @@ from rest_framework import serializers from django.utils.translation import ugettext_lazy as _ from django.core.exceptions import ObjectDoesNotExist -from common.utils import get_logger, is_uuid +from common.utils import get_logger, is_uuid, get_object_or_none from assets.models import Asset logger = get_logger(__file__) @@ -14,22 +14,26 @@ logger = get_logger(__file__) __all__ = ['RemoteAppSerializer'] -class CharPrimaryKeyRelatedField(serializers.PrimaryKeyRelatedField): +class AssetCharPrimaryKeyRelatedField(serializers.PrimaryKeyRelatedField): def to_internal_value(self, data): instance = super().to_internal_value(data) return str(instance.id) - def to_representation(self, value): - # value is instance.id + def to_representation(self, _id): + # _id 是 instance.id if self.pk_field is not None: - return self.pk_field.to_representation(value) - return value + return self.pk_field.to_representation(_id) + # 解决删除资产后,远程应用更新页面会显示资产ID的问题 + asset = get_object_or_none(Asset, id=_id) + if asset: + return None + return _id class RemoteAppSerializer(serializers.Serializer): asset_info = serializers.SerializerMethodField() - asset = CharPrimaryKeyRelatedField( + asset = AssetCharPrimaryKeyRelatedField( queryset=Asset.objects, required=False, label=_("Asset"), allow_null=True ) path = serializers.CharField(