xadmin导入excel
来源:13-8 excel导入插件介绍
repiak
2017-05-10
老师,请知道一下在xadmin中,导入excel的具体逻辑,希望有个demo参考,当获取到request.file后,这个文件是存储在服务器还是到了缓存?
2回答
-
bobby
2017-05-11
def post(self, request, *args, **kwargs):
if 'excel' in request.FILES:
upload_redcord = UploadRecord()
upload_redcord.user = request.user
upload_redcord.file_name = request.FILES['excel'].name
upload_redcord.upload_type = "detection"
upload_redcord.save()
upload_redcord.upload_file.save(request.FILES['excel'].name, request.FILES['excel'])
try:
pici_num = request.FILES['excel'].name.split(".")[0].split("_")[1]
if len(pici_num) != 10 or not pici_num.startswith(u"ZL"):
upload_redcord.process = u"文件名出错"
upload_redcord.message = u"文件名错误, 正确的文件名格式为:检测数据_ZL20160806"
upload_redcord.save()
else:
upload_redcord.pici_num = pici_num
upload_redcord.save()
arc_download = ArchiveDownload()
arc_download.user = request.user
arc_download.task_type = 1
arc_download.pici_num = pici_num
arc_download.save()
except Exception as e:
upload_redcord.process = u"文件名出错"
upload_redcord.message = u"文件名错误, 正确的文件名格式为:ZL20160806"
upload_redcord.save()
return super(UserDetectionAdmin, self).post(request, args, kwargs)你参考一下这段代码
062018-02-13 -
repiak
提问者
2017-05-13
#先在环境中pip install xlrd import xlrd from .models import Course from organization.models import CourseOrg def post(self, request, *args, **kwargs): if 'excel' in request.FILES: #初始化course course = Course() #读取excel文件 data = xlrd.open_workbook(request.FILES['excel'].name) #获取excel第一个表 table = data.sheets()[0] #获取该表行数 nrows = table.nrows #第一行一般为表头,故从该表第二行开始循环取值 for j in range(1, nrows): #获取机构名称,根据表具体内容调整下标 courseorg = table.row_values(j)[0] #通过excel中机构名称中文向CourseOrg查询外键 course.org_id= CourseOrg.objects.get(name=courseorg).id #获取其他字段值,根据表具体内容调整下标 course.name = table.row_values(j)[1] course.desc = table.row_values(j)[2] course.image = table.row_values(j)[3] ………… #保存数据 course.save() return super(CourseAdmin, self).post(request, args, kwargs)
522019-07-15
相似问题