【Django Admin】自定义上传文件
from import_export.formats.base_formats import CSV, XLSX
from django.http import HttpResponse
from import_export.admin import ImportMixin
import pandas as pd
# 定义获取资源格式
class OrderResource(resources.ModelResource):
class Meta:
model = Order
@admin.register(Order)
class OrderAdmin(ImportMixin, admin.ModelAdmin):
resource_class = OrderResource
# 限制获取对应格式
def get_import_formats(self):
return [XLSX]
# 获取上传文件对象
def import_action(self, request, *args, **kwargs):
if request.method == 'POST':
import_file = request.FILES['import_file']
file_name = str(import_file).split(".")[0] # 读取文本名字
# 读取文件对象
df = pd.read_excel(import_file)
# 判断索引
INDEX = 1 if str(next(df.iloc[1:].iterrows())[1][0]).isdigit() else 2
for index, row in df.iloc[INDEX:].iterrows():
print("逻辑")
return HttpResponse('解析成功')
return super().import_action(request, *args, **kwargs)
Python全栈(后端、数据分析、脚本、爬虫、EXE客户端) / 前端(WEB,移动,H5) / Linux / SpringBoot / 机器学习