From 7ae395f7e87c89bd5fd509a3109759ab5f94d3c6 Mon Sep 17 00:00:00 2001 From: ibuler Date: Thu, 1 Sep 2022 21:00:04 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BF=AE=E6=94=B9=20metadata?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/assets/serializers/asset/common.py | 8 ++++---- apps/common/drf/fields.py | 5 +++-- apps/common/drf/metadata.py | 13 ++++++++++++- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/apps/assets/serializers/asset/common.py b/apps/assets/serializers/asset/common.py index 11c800224..439d96483 100644 --- a/apps/assets/serializers/asset/common.py +++ b/apps/assets/serializers/asset/common.py @@ -6,7 +6,7 @@ from django.db.transaction import atomic from django.db.models import F from common.drf.serializers import JMSWritableNestedModelSerializer -from common.drf.fields import LabeledChoiceField, ObjectedRelatedField +from common.drf.fields import LabeledChoiceField, ObjectRelatedField from ..account import AccountSerializer from ...models import Asset, Node, Platform, Protocol, Label, Domain from ...const import Category, AllTypes @@ -45,9 +45,9 @@ class AssetPlatformSerializer(serializers.ModelSerializer): class AssetSerializer(JMSWritableNestedModelSerializer): category = LabeledChoiceField(choices=Category.choices, read_only=True, label=_('Category')) type = LabeledChoiceField(choices=AllTypes.choices, read_only=True, label=_('Type')) - domain = ObjectedRelatedField(required=False, queryset=Domain.objects, label=_('Domain')) - platform = ObjectedRelatedField(required=False, queryset=Platform.objects, label=_('Platform')) - nodes = ObjectedRelatedField(many=True, required=False, queryset=Node.objects, label=_('Nodes')) + domain = ObjectRelatedField(required=False, queryset=Domain.objects, label=_('Domain')) + platform = ObjectRelatedField(required=False, queryset=Platform.objects, label=_('Platform')) + nodes = ObjectRelatedField(many=True, required=False, queryset=Node.objects, label=_('Nodes')) labels = AssetLabelSerializer(many=True, required=False, label=_('Labels')) accounts = AccountSerializer(many=True, required=False, label=_('Accounts')) protocols = AssetProtocolsSerializer(many=True, required=False, label=_('Protocols')) diff --git a/apps/common/drf/fields.py b/apps/common/drf/fields.py index a61a59b04..31386b0a0 100644 --- a/apps/common/drf/fields.py +++ b/apps/common/drf/fields.py @@ -10,7 +10,7 @@ from common.utils import decrypt_password __all__ = [ 'ReadableHiddenField', 'EncryptedField', 'LabeledChoiceField', - 'ObjectedRelatedField', + 'ObjectRelatedField', ] @@ -63,9 +63,10 @@ class LabeledChoiceField(ChoiceField): return super(LabeledChoiceField, self).to_internal_value(data) -class ObjectedRelatedField(serializers.RelatedField): +class ObjectRelatedField(serializers.RelatedField): def __init__(self, **kwargs): self.attrs = kwargs.pop('attrs', None) or ('id', 'name') + self.many = kwargs.get('many', False) super().__init__(**kwargs) def to_representation(self, value): diff --git a/apps/common/drf/metadata.py b/apps/common/drf/metadata.py index e84731321..939c1f314 100644 --- a/apps/common/drf/metadata.py +++ b/apps/common/drf/metadata.py @@ -90,8 +90,19 @@ class SimpleMetadataWithFilters(SimpleMetadata): for choice_value, choice_name in dict(field.choices).items() ] - if field.__class__.__name__ == 'LabeledChoiceField': + class_name = field.__class__.__name__ + if class_name == 'LabeledChoiceField': field_info['type'] = 'labeled_choice' + elif class_name == 'ObjectRelatedField': + field_info['type'] = 'object_related_field' + elif class_name == 'ManyRelatedField': + child_relation_class_name = field.child_relation.__class__.__name__ + if child_relation_class_name == 'ObjectRelatedField': + field_info['type'] = 'm2m_related_field' + + # if field.label == '系统平台': + # print("Field: ", class_name, field, field_info) + return field_info def get_filters_fields(self, request, view):