perf: 优化资产平台的获取

pull/9970/head
ibuler 2 years ago
parent 48b937d867
commit 2b7f90349c

@ -158,7 +158,7 @@ class AssetSerializer(BulkOrgResourceModelSerializer, WritableNestedModelSeriali
self._init_field_choices() self._init_field_choices()
def _get_protocols_required_default(self): def _get_protocols_required_default(self):
platform = self._initial_data_platform platform = self._asset_platform
platform_protocols = platform.protocols.all() platform_protocols = platform.protocols.all()
protocols_default = [p for p in platform_protocols if p.default] protocols_default = [p for p in platform_protocols if p.default]
protocols_required = [p for p in platform_protocols if p.required or p.primary] protocols_required = [p for p in platform_protocols if p.required or p.primary]
@ -214,20 +214,22 @@ class AssetSerializer(BulkOrgResourceModelSerializer, WritableNestedModelSeriali
instance.nodes.set(nodes_to_set) instance.nodes.set(nodes_to_set)
@property @property
def _initial_data_platform(self): def _asset_platform(self):
if self.instance:
return self.instance.platform
platform_id = self.initial_data.get('platform') platform_id = self.initial_data.get('platform')
if isinstance(platform_id, dict): if isinstance(platform_id, dict):
platform_id = platform_id.get('id') or platform_id.get('pk') platform_id = platform_id.get('id') or platform_id.get('pk')
platform = Platform.objects.filter(id=platform_id).first()
if not platform_id and self.instance:
platform = self.instance.platform
else:
platform = Platform.objects.filter(id=platform_id).first()
if not platform: if not platform:
raise serializers.ValidationError({'platform': _("Platform not exist")}) raise serializers.ValidationError({'platform': _("Platform not exist")})
return platform return platform
def validate_domain(self, value): def validate_domain(self, value):
platform = self._initial_data_platform platform = self._asset_platform
if platform.domain_enabled: if platform.domain_enabled:
return value return value
else: else:

Loading…
Cancel
Save