在使用django-admin带来直接管理数据库带来的便利的同时,我们希望数据能批量上传,为了达到此目的,我们需要django-admin-export 模块
配置前:
一、安装模块
pip install django-import-export
二、settings.py注册模块
INSTALLED_APPS = [
....
'import_export'
....
]
三、创建resource.py进行配置
先来看看我的 model ,然后再该model文件夹创建resource.py文件
from django.db import models
# Create your models here.
class OfficialRatingGrade(models.Model):
id = models.IntegerField(primary_key=True)
name = models.CharField(max_length=100, blank=True, null=True)
industy_type = models.CharField(max_length=100, blank=True, null=True)
classify = models.CharField(max_length=100, blank=True, null=True)
grade = models.CharField(max_length=100, blank=True, null=True)
word_frequency_score = models.FloatField(blank=True, null=True)
class Meta:
db_table = 'Official_Rating_Grade'
resource.py配置
from import_export import resources
from .models import *
class PersonResource(resources.ModelResource):
class Meta:
model = Grade
admin.py配置
from django.contrib import admin
from .models import *
from import_export.admin import ImportExportModelAdmin
@admin.register(Grade)
class PersonAdmin(ImportExportModelAdmin):
pass
配置之后,运行
python manage.py runserver
我们可以操作
如图所示: