Django admin后台上传excel,并保存数据到数据库
models.py
class ImportFile(models.Model):
file = models.FileField(upload_to='media')
admin.py
from .models import ImportFile
@admin.register(ImportFile)
class ImportFileAdmin(admin.ModelAdmin):
list_display = ('file', )
# 重写save_model
def save_model(self, request, obj, form, change):
res = super(ImportFileAdmin, self).save_model(request, obj, form, change)
import_user(self, request, obj, change)
return res
appmul下新建utils.py 内容如下
from openpyxl import Workbook, load_workbook
import datetime
from birthapp import models
def import_user(self, request, obj, change):
# 拿到上传文件的obj最相应逻辑处理,obj相当于 models.objects.表.get(id=id)
print(obj.file.path)
w