django建站过程(5)添加导入导出功能

django建站过程(5)添加导入导出功能

后端添加导入导出功能

django-import-export

参考官方:

Installation and configuration --- django-import-export 3.3.4.dev0 documentation

安装命令

复制代码
pip install django-import-export

settings.py添加到INSTALLED_APPS

python 复制代码
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'baseapps.apps.BaseappsConfig',     #或者使用 baseapps
    'mdeditor',
    'import_export',	#加入应用

]

创建一个resource.py

在app所在目录下创建resource.py,代码定义两个类

python 复制代码
from import_export import resources
from baseapps.models import topic,documentes

class topicResource(resources.ModelResource):
    
    class Meta:
        model = topic

class documentesResource(resources.ModelResource):

    class Meta:
        model = documentes

配置Admin

修改admin.py,修改的地方就是把我们定义的Admin类原本继承的admin.ModelAdmin改成ImportExportModelAdmin

python 复制代码
from django.contrib import admin
from baseapps.resource import topicResource,documentesResource      #添加resource.py中的类
from baseapps.models import topic,documentes
from import_export.admin import ImportExportModelAdmin              #导入导出功能
# Register your models here.

class topicAdmin(ImportExportModelAdmin):       # ExportMixin导出,ImportMixin导入,ImportExportModelAdmin导入导出
    resource_class = topicResource              # 引入设定的导出字段,现在是全部
admin.site.register(topic,topicAdmin)

# class docAdmin(admin.ModelAdmin):
class docAdmin(ImportExportModelAdmin):         # ExportMixin导出,ImportMixin导入,ImportExportModelAdmin导入导出
    list_display = ('title','author','date_added','topic')     # ModelAdmin列表页展示的字段名
    search_fields = ('title','author',)      #添加"标题"查询
    list_filter = ('topic',)  # 直接激活过滤器
    resource_class =documentesResource          ## 引入设定的导出字段

admin.site.register(documentes,docAdmin)   # docAdmin只有注册后才能使用

定义导入导出的字段

export_order是导出的字段顺序,fields是定义字段导入(注意字段是否能为空)

python 复制代码
class documentesResource(resources.ModelResource):

    class Meta:
        model = documentes
        fields = ('id', 'topic', 'title', 'date_added','text',)     #定义需要导入哪些字段
        
        export_order = ('id', 'title','topic',  'date_added','text',)   #定义导出的字段顺序
        

其他

python 复制代码
exclude = ['id']											#导入时排除id主键
import_id_fields = ['id']			#定义主键id,这样在id同样的情况下就是更新而不是增加新记录了
id = Field(attribute='id', column_name='编号')		#定义导出时列名为中文'编号'
name = Field(attribute='title', column_name='书籍名称')		
# 定义导出时列名为中文'标题',也可以用column_name=documentes.title.field.verbose_name
相关推荐
编程大师哥几秒前
JavaScript 和 Python 哪个更适合初学者?
开发语言·javascript·python
aiguangyuan1 分钟前
从零构建字符级RNN:用PyTorch实现莎士比亚风格文本生成
人工智能·python·nlp
梦幻精灵_cq17 分钟前
《双征color》诗解——梦幻精灵_cq对终端渲染的数据结构设计模型式拓展
数据结构·python
梅梅绵绵冰22 分钟前
springboot初步2
java·spring boot·后端
喵手35 分钟前
Python爬虫零基础入门【第八章:项目实战演练·第3节】上线与运维入门:定时运行、日志轮转、失败告警(轻量版)!
爬虫·python·爬虫实战·python爬虫工程化实战·零基础python爬虫教学·定时运行·日志轮转
weixin_660096781 小时前
flash-attention总是安装失败
python·flash-attention
yaoxin5211231 小时前
303. Java Stream API - 查找元素
java·windows·python
子午1 小时前
【2026计算机毕设】蔬菜识别系统~Python+深度学习+人工智能+算法模型+TensorFlow
人工智能·python·深度学习
kong79069281 小时前
Python 调用大模型(LLM)
人工智能·python·大模型llm
深蓝电商API1 小时前
Selenium 爬取 Canvas 渲染的数据图表
爬虫·python·selenium