perf: 修改 metadata

pull/8873/head
ibuler 2022-09-01 21:00:04 +08:00
parent 81219e1e7c
commit 7ae395f7e8
3 changed files with 19 additions and 7 deletions

View File

@ -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'))

View File

@ -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):

View File

@ -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):