django后台管理中导出Excel表格与其它表格数据等功能

需求,在django admin后台中添加导出excel表格功能

需求人群:财务,董事

复制代码
# 1导出excel
# 2设定导出的字段
# 3设定导出的表头字段显示中文  
# 4设定导出的字段顺序
# 5权限:超级管理员与用于全国订单查看权限的人
# 6能够筛选字段,然后导出

1 安装django框架中导出Excel表格的插件

c 复制代码
ip install django-import-export  

该插件允许导出文件的格式为:xls,xlsx,csv,tsv,ods,json,yaml,html

执行该命令将会安装以下插件

c 复制代码
defusedxml-0.7.1  
diff-match-patch-20230430  
django-import-export-3.3.6  
et-xmlfile-1.1.0  
markuppy-1.14 odfpy-1.4.1  
openpyxl-3.1.2 pyyaml-6.0.1  
tablib-3.5.0 xlrd-2.0.1 xlwt-1.3.0  

![[Pasted image 20240119043040.png]]

安装插件后,需要添加到应用列表中

c 复制代码
2 在django settings.py 中的INSTALLED_APPS中添加"import_export"这个插件,并且需要在'django.contrib.admin'之前添加

![[Pasted image 20240119043224.png]]

3在订单models.py相同路径中创建resources.py文件

![[Pasted image 20240119043755.png]]

4修改admin.py中的参数与使用插件

![[Pasted image 20240119044610.png]]

5:效果

![[Pasted image 20240119044745.png]]

![[Pasted image 20240119044806.png]]

![[Pasted image 20240119044836.png]]

csv格式

![[Pasted image 20240119044908.png]]

tsv格式

![[Pasted image 20240119061928.png]]

xls格式

![[Pasted image 20240119044923.png]]

html格式

![[Pasted image 20240119061437.png]]

yaml格式

![[Pasted image 20240119061535.png]]

json格式

![[Pasted image 20240119061615.png]]

ods格式

![[Pasted image 20240119061705.png]]

这样还不够,并没有满足需求,我希望是以下图片这样的效果

1:导出的表头为我设定的中文字段

![[Pasted image 20240119060255.png]]

修改resources.py文件

python 复制代码
from import_export import resources  
from .order_models import Order  
  
  
class OrderResource(resources.ModelResource):  
    class Meta:  
        # 模型是哪个  
        model = Order  
        # 要什么字段的数据? 不写则默认全部  
        # fields=()  
        # 当使用了默认全部 fields时,也会有些字段不需要,那么就要使用exclude,表示不需要的字段  
        # exclude=('id',)  
        # 因为导出的外键是数值,并不能显示相关信息,所以需要重新定义字段获取外键中的明显字段  
        fields = ('id',  
                  'userid__user__username',  
                  'trader_userid__user__username',  
                  'team_leader_userid__user__username',  
                  'urban_userid__user__username',  
                  'province_userid__user__username',  
                  'licensing_userid__user__username',  
                  'order_nationally__name',  
                  'order_province__name',  
                  'order_urban__name',  
                  'order_team__name',  
                  'store__name',  
                  'goodsid__name',  
                  'created_at_time',  
                  'updated_at',  
                  'douyinhao',  
                  'douyinname',  
                  'hezuoma',  
                  'shoujihao',  
                  'weixin',  
                  'douyin_have_num',  
                  'pay_method',  
                  'order_price',  
                  'offline_pay_method',  
                  'offline_price',  
                  'begin_show_time',  
                  'no_appointments',  
                  'authorization',  
                  'executestate',  
                  'chargebacks',  
                  'refundsuccessful',  
                  'sale_remark',  
                  'leader_remark',  
                  'licensing_remark',  
                  'trader_remark',  
                  'manage_remark',  
                  'promotion_cost',  
                  )  
        # 设置导出的顺序  
        export_order = ('id',  
                        'userid__user__username',  
                        'trader_userid__user__username',  
                        'team_leader_userid__user__username',  
                        'urban_userid__user__username',  
                        'province_userid__user__username',  
                        'licensing_userid__user__username',  
                        'order_nationally__name',  
                        'order_province__name',  
                        'order_urban__name',  
                        'order_team__name',  
                        'store__name',  
                        'goodsid__name',  
                        'created_at_time',  
                        'updated_at',  
                        'douyinhao',  
                        'douyinname',  
                        'hezuoma',  
                        'shoujihao',  
                        'weixin',  
                        'douyin_have_num',  
                        'pay_method',  
                        'order_price',  
                        'offline_pay_method',  
                        'offline_price',  
                        'begin_show_time',  
                        'no_appointments',  
                        'authorization',  
                        'executestate',  
                        'chargebacks',  
                        'refundsuccessful',  
                        'sale_remark',  
                        'leader_remark',  
                        'licensing_remark',  
                        'trader_remark',  
                        'manage_remark',  
                        'promotion_cost',  
                        )  
  
    def get_export_headers(self):  
        headers = []  
        for field_name in self.get_export_order():  
            headers.append(self.get_export_field_name(field_name))  
        return headers  
  
    def get_export_field_name(self, field_name):  
        column_names = {  
            'id': '订单id号',  
            'userid__user__username': '下单人',  
            'trader_userid__user__username': '投流人',  
            'team_leader_userid__user__username': '小组长',  
            'urban_userid__user__username': '市经理',  
            'province_userid__user__username': '省负责人',  
            'licensing_userid__user__username': '授权人',  
            'order_nationally__name': '国部门',  
            'order_province__name': '省部门',  
            'order_urban__name': '市部门',  
            'order_team__name': '组部门',  
            'store__name': '商店名',  
            'goodsid__name': '商品名',  
            'created_at_time': '创建时间',  
            'updated_at': '更新时间',  
            'douyinhao': '抖音号',  
            'douyinname': '抖音名',  
            'hezuoma': '合作码',  
            'shoujihao': '手机号',  
            'weixin': '微信号',  
            'douyin_have_num': '下单前已有粉丝数量',  
            'pay_method': '线上支付方式',  
            'order_price': '订单金额',  
            'offline_pay_method': '线下支付方式',  
            'offline_price': '线下收款金额',  
            'begin_show_time': '开播时间',  
            'no_appointments': '预约与否',  
            'authorization': '授权状态',  
            'executestate': '投流状态',  
            'chargebacks': '退单',  
            'refundsuccessful': '退单',  
            'sale_remark': '销售备注',  
            'leader_remark': '组长备注',  
            'licensing_remark': '授权人备注',  
            'trader_remark': '投流手备注',  
            'manage_remark': '市经理备注',  
            'promotion_cost': '投流成本',  
            'dingdanyingli': '本单盈利',  
        }  
        return column_names.get(field_name, field_name)
相关推荐
2301_803875611 小时前
PHP 中处理会话数组时的类型错误解析与修复指南
jvm·数据库·python
m0_743623921 小时前
c++如何批量修改文件后缀名_std--filesystem--replace_extension【实战】
jvm·数据库·python
2501_914245932 小时前
CSS如何处理CSS变量作用域冲突_利用特定类名重写变量值
jvm·数据库·python
菜鸟学Python2 小时前
Python生态在悄悄改变:FastAPI全面反超,Django和Flask还行吗?
开发语言·python·django·flask·fastapi
maqr_1104 小时前
MySQL数据库迁移到云端如何保障安全_数据加密与SSL连接配置
jvm·数据库·python
u0109147604 小时前
MySQL如何限制触发器递归调用的深度_防止触发器死循环方法
jvm·数据库·python
weixin_381288184 小时前
MySQL中如何使用HEX函数转换十六进制_MySQL进制转换函数
jvm·数据库·python
Deitymoon4 小时前
嵌入式数据库——SQLite基础
数据库·sqlite
YMatrix 官方技术社区4 小时前
美国·硅谷|YMatrix 即将亮相 Postgres Conference 2026,前瞻 AI 时代的数据基座
数据库·数据仓库·postgresql·时序数据库·ymatrix
bKYP953cL4 小时前
构建自己的AI编程助手:基于RAG的上下文感知实现方案
数据库·人工智能·ai编程