From 1a9cd2dee235cf81c23239a0ed9ca59219e585c6 Mon Sep 17 00:00:00 2001 From: ibuler Date: Thu, 23 Feb 2023 17:44:38 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=E8=B5=84=E4=BA=A7?= =?UTF-8?q?=E6=89=B9=E9=87=8F=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/assets/serializers/asset/common.py | 3 +-- apps/perms/api/user_permission/assets.py | 3 +-- apps/perms/serializers/user_permission.py | 12 +++++++++++- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/apps/assets/serializers/asset/common.py b/apps/assets/serializers/asset/common.py index c4f2a5da7..01a8bc7b8 100644 --- a/apps/assets/serializers/asset/common.py +++ b/apps/assets/serializers/asset/common.py @@ -11,7 +11,6 @@ from accounts.serializers import AccountSerializerCreateValidateMixin from accounts.serializers import AuthValidateMixin from common.serializers import WritableNestedModelSerializer, SecretReadableMixin, CommonModelSerializer from common.serializers.fields import LabeledChoiceField -from common.utils import lazyproperty from orgs.mixins.serializers import BulkOrgResourceModelSerializer from ...const import Category, AllTypes from ...models import Asset, Node, Platform, Label, Protocol @@ -205,7 +204,7 @@ class AssetSerializer(BulkOrgResourceModelSerializer, WritableNestedModelSeriali nodes_to_set.append(node) instance.nodes.set(nodes_to_set) - @lazyproperty + @property def _initial_data_platform(self): if self.instance: return self.instance.platform diff --git a/apps/perms/api/user_permission/assets.py b/apps/perms/api/user_permission/assets.py index 98339fde3..0daa23791 100644 --- a/apps/perms/api/user_permission/assets.py +++ b/apps/perms/api/user_permission/assets.py @@ -29,13 +29,12 @@ class BaseUserPermedAssetsApi(SelfOrPKUserMixin, ListAPIView): ordering_fields = ("name", "address") filterset_class = AssetFilterSet serializer_class = serializers.AssetPermedSerializer - only_fields = serializers.AssetPermedSerializer.Meta.only_fields def get_queryset(self): if getattr(self, 'swagger_fake_view', False): return Asset.objects.none() assets = self.get_assets() - assets = assets.prefetch_related('platform').only(*self.only_fields) + assets = self.serializer_class.setup_eager_loading(assets) return assets @abc.abstractmethod diff --git a/apps/perms/serializers/user_permission.py b/apps/perms/serializers/user_permission.py index 27fde71d1..34143d936 100644 --- a/apps/perms/serializers/user_permission.py +++ b/apps/perms/serializers/user_permission.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- # +from django.db.models import F from django.utils.translation import ugettext_lazy as _ from rest_framework import serializers @@ -32,9 +33,18 @@ class AssetPermedSerializer(OrgResourceModelSerializerMixin): "id", "name", "address", 'domain', 'platform', "comment", "org_id", "is_active", ] - fields = only_fields + ['protocols', 'category', 'type', 'spec_info'] + ['org_name'] + fields = only_fields + ['protocols', 'category', 'type'] + ['org_name'] read_only_fields = fields + @classmethod + def setup_eager_loading(cls, queryset): + """ Perform necessary eager loading of data. """ + queryset = queryset.prefetch_related('domain', 'nodes', 'labels') \ + .prefetch_related('platform', 'protocols') \ + .annotate(category=F("platform__category")) \ + .annotate(type=F("platform__type")) + return queryset + class NodePermedSerializer(serializers.ModelSerializer): class Meta: