码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 【Django项目记录:django admin后台将查询结果导出表格】


    Django项目记录:django admin后台将数据导出表格

    • Django admin后台导出
      • 首先安装导出的插件(包)
      • 根据查询结果导出代码,在app文件夹中添加文件resources.py
      • 修改相对应的app的admin后端页面(admin.py)

    Django admin后台导出

    首先安装导出的插件(包)

    pip install django-import-export
    
    • 1

    根据查询结果导出代码,在app文件夹中添加文件resources.py

    from import_export import resources
    from .models import *
    # 1,列表导出的使用: 重写方法 get_export_order 指定了导出时列的顺序按照 Meta中fields的顺序
    # 2,列表导出的使用: 重写方法 get_export_headers 指定了导出时列的标题
    class QueryResultsResource(resources.ModelResource):
        def dehydrate_course_type(self, obj):
            return obj.get_course_type_display()
        class Meta:
            model = AttendanceHistory
            fields = ['duty_teacher__name', 'course_type', 'attendance__role','class_time','current_subject','number_change','number_left']  # 选择要导出的字段
        def get_export_headers(self):  # 指定表格的标题
            headers = ['上课老师','课程类别','考勤类别','上课时间','课程主题','课次变化','剩余课次']
            return headers
        def get_export_order(self): # 指定表格中导出字段的顺序
            return self._meta.fields
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    修改相对应的app的admin后端页面(admin.py)

    from django.contrib import admin
    from .models import *
    from import_export.admin import ExportMixin
    from .resources import *
    # Register your models here.
    class AttendanceHistoryAdmin(ExportMixin, admin.ModelAdmin):
        resource_class = QueryResultsResource # 在admin中添加并使用导出功能
        search_fields = ['student__name','student__phone']
        list_filter = ['duty_teacher','class_time','course_type','attendance']
        list_display = ['id','student', 'teacher','duty_teacher', 'course_type','attendance', 'number_change', 
                        'attendance_class', 'create_at', 'update_at','class_time','number_left']
                        
    admin.site.register(AttendanceHistory, AttendanceHistoryAdmin)                    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
  • 相关阅读:
    什么情况?周鸿祎和三六零违约,“零元”甩卖哪吒汽车10亿元股权
    【STM32外设系列】NRF24L01无线收发模块
    序列图怎么画,也就是顺序图
    CentOS7+Linux version 3.10.0-693.2.2.el7.x86_64 上安装docker
    浅谈——“总线系统”
    Go 常量为什么只支持基本数据类型?
    armlinux移植ffmepg
    PDCA项目开发环境搭建说明
    python学习12--爬虫
    12月1日(第三天)
  • 原文地址:https://blog.csdn.net/qq_28207005/article/details/133020960
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号