Add CSV export function to management screen with django-import-export

Django's admin site, I found out that there are various plugins.

I wanted to import / export the data on the management screen in a format such as CSV, so When I looked it up, I could easily do it with django-import-export. A memorandum at that time.

Installation

First install with pip

$ pip install django-import-export
Setting

Added ʻimport_export` to INSTALLED_APPS

# settings.py
INSTALLED_APPS = (
    ...
    'import_export',
)
Add to admin.py

Add settings for the target data.

The sample model looks like this.

# models.py
class Book(models.Model):
    name = models.CharField('Book name', max_length=100)
    author = models.CharField('Book name', max_length=100)

django-import-export settings.

Add a class that inherits ModelResource for the target model. It seems that setting related items will be written here.

# admin.py
from django.contrib import admin
from import_export import resources
from import_export.admin import ImportExportModelAdmin

from .models import Book

class BookResource(resources.ModelResource):
    #Django for Model-import-export settings
    class Meta:
        model = Book


@admin.register(Book)
class BookAdmin(ImportExportModelAdmin):
    #Use ImportExportModelAdmin
    ordering = ['id']
    list_display = ('id', 'title', 'author')

    # django-import-exports settings
    resource_class = BookResource

Finally, prepare an Admin class that inherits ImportExportModelAdmin, If you set resource_class to a class that inherits ModelResource, it's OK!

Then, the button is displayed like this. Easy (* ´ω ` *)

スクリーンショット 2019-11-27 13.38.26.png

Small story

Export only: Disable Import

I thought I didn't need to import it separately, so I disabled it. It seems that only ʻExport Mixin` should be used.

# ...Abbreviation
from import_export.admin import ExportMixin

@admin.register(Book)
class BookAdmin(ExportMixin, admin.ModelAdmin):
    #Admin to ExportMixin.OK if you add it to ModelAdmin
    ordering = ['id']
    list_display = ('id', 'title', 'author')

    # django-import-exports settings
    resource_class = BookResource
Specify the format that can be exported

By default, you can choose JSON or YML, but CSV is all you need. I narrowed down the parts that can be selected. OK if you specify formats

# ...Abbreviation
from import_export.formats import base_formats

@admin.register(Book)
class BookAdmin(ImportExportModelAdmin):
    ordering = ['id']
    list_display = ('id', 'title', 'author')

    # django-import-exports settings
    resource_class = BookResource
    formats = [base_formats.CSV] #Can be specified in formats

that's all!!

I'm making something like this !!

We have released "Tsundoku How Match", a reading management app for Tsundoku! Tsundoku How Match is developed with Nuxt.js + Firebase!

If you like, please play ヽ (= ´ ▽ `=) ノ

If you have any requests, impressions, advice, etc. To the official account (@MemoryLoverz) and the developer (@kira_puka) ♪

Referenced site

Recommended Posts

Add CSV export function to management screen with django-import-export
Try using django-import-export to add csv data to django
Write to csv with Python
Add fields to features with ArcPy
[Python] Write to csv file with Python
Handle csv files with Django (django-import-export)
Output to csv file with Python
Excel, csv import, export with Django
I tried to create CSV upload, data processing, download function with Django
How to add a package with PyCharm
Convert from PDF to CSV with pdfplumber
Add images to iOS photos with Pythonista
Read Python csv and export to txt
[Part1] Scraping with Python → Organize to csv!
Add Gaussian noise to images with python2.7
How to create sample CSV data with hypothesis
Scraping tabelog with python and outputting to CSV
Add your favorite display function to pip search
Add rows to an empty array with numpy
How to add arbitrary headers to response with FastAPI
Export RDS snapshot to S3 with Lambda (Python)
Write CSV data to AWS-S3 with AWS-Lambda + Python
Add / remove kernel to use jupyter with venv
Output test function docstring to report with pytest-html