From 799642f1b16048aab8f7b29412834c8fbca3d9fd Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E6=9D=8E=E5=BC=BA?= <1206709430@qq.com>
Date: Fri, 19 May 2023 16:02:34 +0800
Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=EF=BC=9A=E5=AF=BC=E5=85=A5?=
 =?UTF-8?q?=E6=A8=A1=E6=9D=BF=E4=BC=98=E5=8C=96?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 backend/dvadmin/utils/import_export.py       | 18 +++++++++---------
 backend/dvadmin/utils/import_export_mixin.py |  3 +++
 2 files changed, 12 insertions(+), 9 deletions(-)

diff --git a/backend/dvadmin/utils/import_export.py b/backend/dvadmin/utils/import_export.py
index 2bd6e1e..320c72f 100644
--- a/backend/dvadmin/utils/import_export.py
+++ b/backend/dvadmin/utils/import_export.py
@@ -21,9 +21,9 @@ def import_to_data(file_url, field_data, m2m_fields=None):
     file_path_dir = os.path.join(settings.BASE_DIR, file_url)
     workbook = openpyxl.load_workbook(file_path_dir)
     table = workbook[workbook.sheetnames[0]]
-    theader = tuple(table.values)[0] #Excel的表头
-    is_update = '更新主键(勿改)' in theader #是否导入更新
-    if is_update is False: #不是更新时,删除id列
+    theader = tuple(table.values)[0]  # Excel的表头
+    is_update = '更新主键(勿改)' in theader  # 是否导入更新
+    if is_update is False:  # 不是更新时,删除id列
         field_data.pop('id')
     # 获取参数映射
     validation_data_dict = {}
@@ -35,9 +35,10 @@ def import_to_data(file_url, field_data, m2m_fields=None):
                 for k, v in choices.get("data").items():
                     data_dict[k] = v
             elif choices.get("queryset") and choices.get("values_name"):
-                data_list = choices.get("queryset").values(choices.get("values_name"), "id")
+                data_list = choices.get("queryset").values(choices.get("values_name"),
+                                                           choices.get("values_value", "id"))
                 for ele in data_list:
-                    data_dict[ele.get(choices.get("values_name"))] = ele.get("id")
+                    data_dict[ele.get(choices.get("values_name"))] = ele.get(choices.get("values_value", "id"))
             else:
                 continue
             validation_data_dict[key] = data_dict
@@ -53,12 +54,11 @@ def import_to_data(file_url, field_data, m2m_fields=None):
             values = items[1]
             value_type = 'str'
             if isinstance(values, dict):
-                value_type = values.get('type','str')
+                value_type = values.get('type', 'str')
             cell_value = table.cell(row=row + 1, column=index + 2).value
-            if cell_value is None or cell_value=='':
+            if cell_value is None or cell_value == '':
                 continue
             elif value_type == 'date':
-                print(61, datetime.strptime(str(cell_value), '%Y-%m-%d %H:%M:%S').date())
                 try:
                     cell_value = datetime.strptime(str(cell_value), '%Y-%m-%d %H:%M:%S').date()
                 except:
@@ -66,7 +66,7 @@ def import_to_data(file_url, field_data, m2m_fields=None):
             elif value_type == 'datetime':
                 cell_value = datetime.strptime(str(cell_value), '%Y-%m-%d %H:%M:%S')
             else:
-            # 由于excel导入数字类型后,会出现数字加 .0 的,进行处理
+                # 由于excel导入数字类型后,会出现数字加 .0 的,进行处理
                 if type(cell_value) is float and str(cell_value).split(".")[1] == "0":
                     cell_value = int(str(cell_value).split(".")[0])
                 elif type(cell_value) is str:
diff --git a/backend/dvadmin/utils/import_export_mixin.py b/backend/dvadmin/utils/import_export_mixin.py
index 44f51cc..4f6a807 100644
--- a/backend/dvadmin/utils/import_export_mixin.py
+++ b/backend/dvadmin/utils/import_export_mixin.py
@@ -1,4 +1,5 @@
 # -*- coding: utf-8 -*-
+from types import FunctionType, MethodType
 from urllib.parse import quote
 
 from django.db import transaction
@@ -68,6 +69,8 @@ class ImportSerializerMixin:
         :return:
         """
         assert self.import_field_dict, "'%s' 请配置对应的导出模板字段。" % self.__class__.__name__
+        if isinstance(self.import_field_dict, MethodType) or isinstance(self.import_field_dict, FunctionType):
+            self.import_field_dict = self.import_field_dict()
         # 导出模板
         if request.method == "GET":
             # 示例数据