django 配置admin 数据管理,增加数据批量上传下载功能
在使用django-admin带来直接管理数据库带来的便利的同时,我们希望数据能批量上传,为了达到此目的,我们需要django-admin-export 模块
一、安装模块
pip3 install django-import-export -i https://mirrors.aliyun.com/pypi/simple/
二、settings.py注册模块
INSTALLED_APPS = [ 'simpleui', 'daterange_filter', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', # 'app01', 'monitor01', 'import_export' ]
三、准备Module数据表,admin后台注册
modules.py
# Resource 资源列表 class Resource(models.Model): name = models.CharField(max_length=256, verbose_name='资源名称')#verbose_name表示admin管理时显示的中文 uid = models.CharField(max_length=32, verbose_name='账号UID') namespace = models.CharField(max_length=32, verbose_name='资源分类(NameSpace)')#verbose_name表示admin管理时显示的中文 instance_id = models.CharField(max_length=64, verbose_name='实例ID') region_select = ( ("cn-beijing-6", "华北1(北京)"), ("cn-shanghai-2", "华东1(上海)"), ("cn-hongkong-2", "中国香港"), ("cn-guangzhou-1", "华南1(广州)"), ("ap-singapore-1", "新加坡"), ("eu-east-1", "俄罗斯(莫斯科)"), ("cn-beijing-fin", "华北金融1(北京)专区"), ("eu-east-1", "华东金融1(上海)专区"), ("cn-southwest-1", "西南1(重庆)"), ("cn-central-1", "华中1(武汉)"), ("cn-northwest-1", "西北1区(庆阳)"), ) ''' https://docs.ksyun.com/documents/6477 ''' region = models.CharField(max_length=32, choices=region_select, verbose_name='所在地区', blank=False) # 关联查询的时候展示的中文名称 def __str__(self): return self.name # admin后台项目中文显示设置 class Meta: verbose_name = "客户资源" verbose_name_plural = "客户资源" db_table = 'monitor01_resource'
admin.py 在admin管理模块中注册
from import_export.admin import ImportExportModelAdmin
from django.contrib import admin # from django.utils.safestring import mark_safe from django.utils.html import format_html # from django.forms.widgets import TextInput from django.db import models as dbmodels # Register your models here. from monitor01 import models from import_export.admin import ImportExportModelAdmin # ResourceAdmin 支持批量导入数据的Admin @admin.register(models.Resource) class ResourceAdmin(ImportExportModelAdmin): list_display = ('region', 'namespace', 'uid', 'name', 'instance_id', ) search_fields = ('namespace', 'uid', 'name', 'instance_id', 'region') list_display_links = ('namespace', 'uid', 'name', 'instance_id', 'region') pass
测试导出导入
导出直接点击导出按钮
导入时去掉ID列
posted on 2023-04-13 10:11 zhangmingda 阅读(309) 评论(0) 编辑 收藏 举报