[PYTHON] Handle csv files with Django (django-import-export)

When I created an app to manage restaurant expenses Since it was necessary to capture past data at once, I found a useful plug-in. I think it can be used when managing a large amount of data such as stock price data in the future.

Development environment

Sample Model

models.py


from django.db import models
from datetime import datetime


class Category(models.Model):
    category_name = models.CharField(max_length=255,unique=True)


class Shop(models.Model):
    shop_name = models.CharField(max_length=255,unique=True)
    flag = models.IntegerField(verbose_name="flag")
    category = models.ForeignKey(Category, on_delete = models.PROTECT, verbose_name="category")


class Cost(models.Model):
    date = models.DateField(verbose_name="date",default=datetime.now)
    shop = models.ForeignKey(Shop, on_delete = models.PROTECT, verbose_name="Store name")
    price = models.IntegerField(verbose_name="Amount of money", help_text="Half-width input")
    memo = models.CharField(verbose_name="Remarks", max_length=500, blank=True)
    created_at = models.DateTimeField(verbose_name='Registered Date', auto_now_add=True)
    updated_at = models.DateTimeField(verbose_name='Update date and time', auto_now=True)

In order to manage the expenses of restaurants, there are three models such as category, supplier, amount, etc. The rest is as in models.py.

Install django-import-export

$ pipenv install django-import-export

The above is when using pipenv. If you install with pip, you can do it with $ pip install django-import-export.

Added ʻimport_export` to settings.py

settings.py


INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    #···abridgement
    'import_export', #Add here
]

Added ʻimport_export` to INSTALLED_APPS in settings.py under the project

Add settings to admin.py

admin.py


from django.contrib import admin
from import_export import resources
from import_export.admin import ImportExportModelAdmin
from .models import Category, Shop, Cost

#Create a class that inherits ModelResource to integrate with the Category model
class CategoryResource(resources.ModelResource):
    class Meta:
        model = Category

#Create a class that inherits ModelResource to integrate with the Shop model
class ShopResource(resources.ModelResource):
    class Meta:
        model = Shop

#Create a class that inherits ModelResource to integrate with the Cost model
class CostResource(resources.ModelResource):
    class Meta:
        model = Cost


@admin.register(Category)
#Create an admin class that inherits ImportExportModelAdmin
class CategoryAdmin(ImportExportModelAdmin):
    ordering = ['id']
    list_display=('id', 'category_name')

    # resource_Set a class that inherits ModelResource in class
    resource_class = CategoryResource


@admin.register(Shop)
#Create an admin class that inherits ImportExportModelAdmin
class ShopAdmin(ImportExportModelAdmin):
    ordering = ['id']
    list_display=('id', 'shop_name', 'flag', 'category')

    # resource_Set a class that inherits ModelResource in class
    resource_class = ShopResource


@admin.register(Cost)
#Create an admin class that inherits ImportExportModelAdmin
class CostAdmin(ImportExportModelAdmin):
    ordering = ['id']
    list_display=('id', 'date', 'shop', 'price', 'memo')

    # resource_Set a class that inherits ModelResource in class
    resource_class = CostResource

A class that inherits ModelResource and ʻCreate an admin classthat inherits ImportExportModelAdmin and Just write a class that inheritsModelResource in resource_class`. To make the defined resource class available on the management screen It may be easier to understand if you think that it is linked with admin.py.

Access any URL / admin

ss.png The import and export buttons are displayed as shown in the image. You can actually import and export with this. ss 1.png

By the way, in addition to csv, it also supports xls, xlsx, tsv, json, and yaml. Surprisingly convenient. However, I feel that the processing speed is a little slow when there is a lot of data. .. ..

Referenced site

https://qiita.com/kira_puka/items/14a1a604a428a4c68884 https://django-import-export.readthedocs.io/en/stable/index.html

Recommended Posts

Handle csv files with Django (django-import-export)
Handle Excel CSV files with Python
Upload files with Django
Handle JSON files with Matlab
How to handle static files when deploying to production with Django
Manage Django config files with Python-decouple
Excel, csv import, export with Django
Handle multiple Django projects with Apache (WSGIDaemonProcess)
Read and write csv files with numpy
Internationalization with django
CRUD with Django
Try using django-import-export to add csv data to django
Handle zip files with Japanese filenames in Python 3
Configure a module with multiple files in Django
Authenticate Google with Django
Django 1.11 started with Python3.6
Development digest with Django
Output PDF with Django
Markdown output with Django
Use Gentelella with django
Twitter OAuth with Django
Getting Started with Django 1
Handle Excel with python
Send email with Django
Handle rabbimq with python
File upload with django
Use LESS with Django
Pooling mechanize with Django
Csv tinkering with python
[Day 6] Handle static files
Use MySQL with Django
[Python] Reading CSV files
Start today with Django
Remove headings from multiple format CSV files with python
Getting Started with Django 2
Create Heroku, Flask, Python, Nyanko bulletin boards with "csv files"
Convert only date serial numbers in CSV files with awk
Read csv with python pandas
Get started with Django! ~ Tutorial ⑤ ~
Create an API with Django
Extract Twitter data with CSV
Do Django with CodeStar (Python3.8, Django2.1.15)
Deploy Django serverless with Lambda
Python3 + Django ~ Mac ~ with Apache
Getting Started with Python Django (1)
Create a homepage with django
Reading and writing csv files
Sorting image files with Python (2)
Get started with Django! ~ Tutorial ④ ~
Sort huge files with python
Sorting image files with Python (3)
Getting Started with Python Django (4)
Web application creation with Django
Getting Started with Python Django (3)
Combine FastAPI with Django ORM
[Tips] Handle Athena with Python
Get started with Django! ~ Tutorial ⑥ ~
Sorting image files with Python
Save tweet data with Django
Transfer files with teraterm [Note]
Integrate PDF files with Python