From b088362ae304d19d2bb7390a50b8991fea9fc784 Mon Sep 17 00:00:00 2001 From: feng626 <1304903146@qq.com> Date: Wed, 16 Feb 2022 18:29:55 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E8=B4=A6=E5=8F=B7=E5=A4=87=E4=BB=BD?= =?UTF-8?q?=E6=80=A7=E8=83=BD=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/assets/task_handlers/backup/handlers.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/apps/assets/task_handlers/backup/handlers.py b/apps/assets/task_handlers/backup/handlers.py index 91bae54d1..d0e9fe365 100644 --- a/apps/assets/task_handlers/backup/handlers.py +++ b/apps/assets/task_handlers/backup/handlers.py @@ -52,10 +52,11 @@ class BaseAccountHandler: return header_fields @classmethod - def create_row(cls, account, serializer_cls): + def create_row(cls, account, serializer_cls, header_fields=None): serializer = serializer_cls(account) + if not header_fields: + header_fields = cls.get_header_fields(serializer) data = cls.unpack_data(serializer.data) - header_fields = cls.get_header_fields(serializer) row_dict = {} for field, header_name in header_fields.items(): row_dict[header_name] = data[field] @@ -74,12 +75,16 @@ class AssetAccountHandler(BaseAccountHandler): def create_df(cls): df_dict = defaultdict(list) sheet_name = AuthBook._meta.verbose_name - accounts = AuthBook.get_queryset() + + accounts = AuthBook.get_queryset().select_related('systemuser') + if not accounts.first(): + return df_dict + + header_fields = cls.get_header_fields(AccountSecretSerializer(accounts.first())) for account in accounts: account.load_auth() - row = cls.create_row(account, AccountSecretSerializer) + row = cls.create_row(account, AccountSecretSerializer, header_fields) df_dict[sheet_name].append(row) - for k, v in df_dict.items(): df_dict[k] = pd.DataFrame(v) @@ -98,7 +103,7 @@ class AppAccountHandler(BaseAccountHandler): @classmethod def create_df(cls): df_dict = defaultdict(list) - accounts = Account.get_queryset() + accounts = Account.get_queryset().select_related('systemuser') for account in accounts: account.load_auth() app_type = account.type