欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Django import export實(shí)現(xiàn)數(shù)據(jù)庫(kù)導(dǎo)入導(dǎo)出方式

 更新時(shí)間:2020年04月03日 08:36:58   作者:一天一點(diǎn)到  
這篇文章主要介紹了Django import export實(shí)現(xiàn)數(shù)據(jù)庫(kù)導(dǎo)入導(dǎo)出方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

使用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',)   # 過(guò)濾條件
 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è)方法可以一次性寫(xiě)入所有列,記不清了,只能用這種簡(jiǎn)單的方法去實(shí)現(xiàn)
   sheet.write(cols,1,str(query.idfa))  # 寫(xiě)入第一列
   sheet.write(cols,2,str(query.day_time)) # 寫(xiě)入第二列
   sheet.write(cols,3,str(query.keyword))  # 寫(xiě)入第三列
   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)文章

  • 解決pymongo連接數(shù)據(jù)庫(kù)報(bào)錯(cuò)certificate verify failed:certificate has expired

    解決pymongo連接數(shù)據(jù)庫(kù)報(bào)錯(cuò)certificate verify failed:certific

    這篇文章主要介紹了解決pymongo連接數(shù)據(jù)庫(kù)報(bào)錯(cuò)certificate verify failed:certificate has expired問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • python爬蟲(chóng)控制aiohttp并發(fā)數(shù)量方式

    python爬蟲(chóng)控制aiohttp并發(fā)數(shù)量方式

    這篇文章主要介紹了python爬蟲(chóng)控制aiohttp并發(fā)數(shù)量方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-06-06
  • Python命令行參數(shù)化的四種方式詳解

    Python命令行參數(shù)化的四種方式詳解

    在日常編寫(xiě) Python 腳本的過(guò)程中,我們經(jīng)常需要結(jié)合命令行參數(shù)傳入一些變量參數(shù),使項(xiàng)目使用更加的靈活方便。本文章羅列了構(gòu)建 Python命令行參數(shù)的4種常見(jiàn)方式,需要的可以參考一下
    2022-06-06
  • tensorflow實(shí)現(xiàn)二維平面模擬三維數(shù)據(jù)教程

    tensorflow實(shí)現(xiàn)二維平面模擬三維數(shù)據(jù)教程

    今天小編就為大家分享一篇tensorflow實(shí)現(xiàn)二維平面模擬三維數(shù)據(jù)教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-02-02
  • python 獲取utc時(shí)間轉(zhuǎn)化為本地時(shí)間的方法

    python 獲取utc時(shí)間轉(zhuǎn)化為本地時(shí)間的方法

    今天小編就為大家分享一篇python 獲取utc時(shí)間轉(zhuǎn)化為本地時(shí)間的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-12-12
  • linux系統(tǒng)下pip升級(jí)報(bào)錯(cuò)的解決方法

    linux系統(tǒng)下pip升級(jí)報(bào)錯(cuò)的解決方法

    這篇文章主要給大家介紹了關(guān)于linux系統(tǒng)下pip升級(jí)報(bào)錯(cuò)的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • 詳解Python當(dāng)中的字符串和編碼

    詳解Python當(dāng)中的字符串和編碼

    這篇文章主要介紹了詳解Python當(dāng)中的字符串和編碼,代碼基于Python2.x版本,文中所述皆是Python學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-04-04
  • Pytorch入門(mén)之mnist分類(lèi)實(shí)例

    Pytorch入門(mén)之mnist分類(lèi)實(shí)例

    這篇文章主要為大家詳細(xì)介紹了Pytorch入門(mén)之mnist分類(lèi)實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-04-04
  • ipython和python區(qū)別詳解

    ipython和python區(qū)別詳解

    在本篇文章里小編給大家分享了關(guān)于ipython和python區(qū)別的相關(guān)知識(shí)點(diǎn),有興趣的朋友們跟著學(xué)習(xí)下。
    2019-06-06
  • 關(guān)于Python中object類(lèi)特殊方法的解釋

    關(guān)于Python中object類(lèi)特殊方法的解釋

    在學(xué)習(xí)Python的過(guò)程中我們會(huì)發(fā)現(xiàn)有一個(gè)類(lèi)?Object類(lèi)?,它是所有類(lèi)的父類(lèi),Object類(lèi)規(guī)定了python用于類(lèi)的內(nèi)置函數(shù),今天我們就來(lái)看看幾個(gè)常用的特殊方法吧
    2023-03-03

最新評(píng)論