Django import export實(shí)現(xiàn)數(shù)據(jù)庫(kù)導(dǎo)入導(dǎo)出方式
使用django-import-export庫(kù),導(dǎo)入導(dǎo)出數(shù)據(jù),支持csv、xls、json、html等格式
官網(wǎng):http://django-import-export.readthedocs.io/en/latest/installation.html
1、安裝django-import-export
pip install django-import-export
2、配置settings.py
INSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'customer', 'publisher', 'import_export', )
執(zhí)行命令: python manage.py collectstatic
3、models.py 建立model
class Author(models.Model):
name = models.CharField(max_length=100)
def __unicode__(self):
return self.name
class Category(models.Model):
name = models.CharField(max_length=100)
def __unicode__(self):
return self.name
class Book(models.Model):
name = models.CharField('Book name', max_length=100)
author = models.ForeignKey(Author, blank=True, null=True)
author_email = models.EmailField('Author email', max_length=75, blank=True)
imported = models.BooleanField(default=False)
published = models.DateField('Published', blank=True, null=True)
price = models.DecimalField(max_digits=10, decimal_places=2, null=True, blank=True)
categories = models.ManyToManyField(Category, blank=True)
def __unicode__(self):
return self.name
4、在admin.py 創(chuàng)建Resource、對(duì)應(yīng)的Admin
from import_export import resources
from core.models import Book
from import_export.admin import ImportExportModelAdmin
class BookResource(resources.ModelResource):
class Meta:
model = Book
export_order = ('id', 'name', 'author', 'author_email', 'imported', 'click', 'published', 'price', 'categories')
@admin.register(Book)
class BookAdmin(ImportExportModelAdmin):
list_display = ('name', 'author', 'author_email', 'imported', 'published', 'price', 'categories')
search_fields = ('name', 'author','published')
date_hierarchy = 'date'
resource_class = BookResource
export_order:設(shè)置導(dǎo)出字段的順序
5、Django界面實(shí)現(xiàn)導(dǎo)入導(dǎo)出

自定義導(dǎo)出 方式 action 這種方式也推薦
import xlwt
#導(dǎo)出Excel
from django.http import StreamingHttpResponse
class AdminReport(admin.ModelAdmin):
actions = ["saveexecl"] # 自定義的action(導(dǎo)出到excel表格)
list_display = ("id",'offer','day_time', 'idfa', 'submit_result_text', 'callback_result_text') # 顯示的列
search_fields = ('day_time','callback_result_text') # 可以搜索的字段
date_hierarchy = 'day_time' # 按照日期顯示
list_filter = ('offer',) # 過濾條件
list_per_page = 500 # 每頁(yè)顯示500條,太多了可能會(huì)出現(xiàn)服務(wù)器崩掉的情況
def saveexecl(self,request,queryset):
Begin = xlwt.Workbook()
sheet = Begin.add_sheet("response")
cols = 0
for query in queryset:
# you need write colms # 好像有個(gè)方法可以一次性寫入所有列,記不清了,只能用這種簡(jiǎn)單的方法去實(shí)現(xiàn)
sheet.write(cols,1,str(query.idfa)) # 寫入第一列
sheet.write(cols,2,str(query.day_time)) # 寫入第二列
sheet.write(cols,3,str(query.keyword)) # 寫入第三列
cols += 1
Begin.save("%s" %(filename))
def file_iterator(filename,chuck_size=512):
with open(filename,"rb") as f:
while True:
c = f.read(chuck_size)
if c:
yield c
else:
break
response = StreamingHttpResponse(file_iterator(filename))
response['Content-Type'] = 'application/octet-stream'
response['Content-Disposition'] = 'attachment;filename="{}"'.format("result.xls")
return response
saveexecl.short_description = "導(dǎo)出Excel" # 按鈕顯示名字
admin.site.register(Report, AdminReport) # 注冊(cè)到admin
以上這篇Django import export實(shí)現(xiàn)數(shù)據(jù)庫(kù)導(dǎo)入導(dǎo)出方式就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python實(shí)現(xiàn)找到同名文件并復(fù)制到其他文件夾中
這篇文章主要為大家介紹了如何基于Python語(yǔ)言,實(shí)現(xiàn)依據(jù)某一文件夾中大量文件的名稱復(fù)制另一文件夾中的同名文件,文中的示例代碼簡(jiǎn)潔易懂,需要的可以參考一下2023-05-05
使用Tensorflow將自己的數(shù)據(jù)分割成batch訓(xùn)練實(shí)例
今天小編就為大家分享一篇使用Tensorflow將自己的數(shù)據(jù)分割成batch訓(xùn)練實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2020-01-01
Python列表對(duì)象實(shí)現(xiàn)原理詳解
這篇文章主要介紹了Python列表對(duì)象實(shí)現(xiàn)原理詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07
python實(shí)現(xiàn)Virginia無(wú)密鑰解密
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)Virginia無(wú)密鑰解密,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-03-03
在Python的Flask中使用WTForms表單框架的基礎(chǔ)教程
WTForms由Python寫成,為表單而生,提供了很多制作Web表單的實(shí)用API,和Flask框架結(jié)合使用效果拔群,這里我們就一起看一下在Python的Flask中使用WTForms表單框架的基礎(chǔ)教程2016-06-06
如何使用yolov5輸出檢測(cè)到的目標(biāo)坐標(biāo)信息
YOLOv5是一系列在 COCO 數(shù)據(jù)集上預(yù)訓(xùn)練的對(duì)象檢測(cè)架構(gòu)和模型,下面這篇文章主要給大家介紹了關(guān)于如何使用yolov5輸出檢測(cè)到的目標(biāo)坐標(biāo)信息的相關(guān)資料,需要的朋友可以參考下2022-03-03

