亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

使用django-import-export庫,導入導出數(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、對應的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:設置導出字段的順序

5、Django界面實現(xiàn)導入導出

自定義導出 方式 action 這種方式也推薦

import xlwt
#導出Excel
from django.http import StreamingHttpResponse
class AdminReport(admin.ModelAdmin):
 actions = ["saveexecl"]     # 自定義的action(導出到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    # 每頁顯示500條,太多了可能會出現(xiàn)服務器崩掉的情況
 
 def saveexecl(self,request,queryset):
  Begin = xlwt.Workbook()
  sheet = Begin.add_sheet("response")
  cols = 0
  for query in queryset:
   # you need write colms      # 好像有個方法可以一次性寫入所有列,記不清了,只能用這種簡單的方法去實現(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 = "導出Excel"   # 按鈕顯示名字


admin.site.register(Report, AdminReport)  # 注冊到admin

以上這篇Django import export實現(xiàn)數(shù)據(jù)庫導入導出方式就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • 解決pymongo連接數(shù)據(jù)庫報錯certificate verify failed:certificate has expired

    解決pymongo連接數(shù)據(jù)庫報錯certificate verify failed:certific

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

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

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

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

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

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

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

    python 獲取utc時間轉化為本地時間的方法

    今天小編就為大家分享一篇python 獲取utc時間轉化為本地時間的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-12-12
  • linux系統(tǒng)下pip升級報錯的解決方法

    linux系統(tǒng)下pip升級報錯的解決方法

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

    詳解Python當中的字符串和編碼

    這篇文章主要介紹了詳解Python當中的字符串和編碼,代碼基于Python2.x版本,文中所述皆是Python學習中的基礎知識,需要的朋友可以參考下
    2015-04-04
  • Pytorch入門之mnist分類實例

    Pytorch入門之mnist分類實例

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

    ipython和python區(qū)別詳解

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

    關于Python中object類特殊方法的解釋

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

最新評論