[PYTHON] Try using django-import-export to add csv data to django

Introduction

In this article, I use the django-import-export library to read the csv file I created by myself into django. django-import-export is a library that allows you to import and export csv files from the management screen. Using this, this time we will create a model such as Book, Publisher, etc., and even read the related (one-to-one or one-to-one) csv file. In addition, I would like to describe the impressions I have used.

Official site: django-import-export

Target person

・ People who want to save the data of their own csv file in the django model ・ People who tried to use django-import-export but didn't work ・ People who know about one-to-one and many-to-one contents of databases

environment

Please install the following packages.

pip install django django-import-export

table of contents

  1. Create a project
  2. Create app
  3. Create csv file
  4. Try importing

Project creation

Basic configuration

django-admin startproject config .
python manage.py startapp Book

Since the project and model can be created with the above command, add additional apps and modules in settings.py.

# config/settings.py

INSTALLED_APPS = [
    "import_export",#django-import-Required to use export
    "Book",#App created this time
]

App

This time, we will create three models. Describe as follows in the models of the Book created earlier.

#Book/models.py
from django.db import models
class Publisher(models.Model):
    name = models.CharField("Publisher name",max_length=100,help_text="the publisher")

class Book(models.Model):
    title = models.CharField("title",max_length=100,help_text="本のtitle")
    publisher = models.ForeignKey(Publisher,on_delete=models.CASCADE)

class BookPrice(models.Model):
    book = models.OneToOneField(Book,on_delete=models.CASCADE)
    price = models.IntegerField("List price",help_text="本のList price")

The contents of the model are as follows. ・ Book has many relationships with Publisher ・ BookPrice and Book have a one-to-one relationship

The turn of django-import-export

To import the csv file into these models, set from the management screen. This is where django-import-export comes in. The concrete method is completed by just playing with admin.

#Book/admin.py
from django.contrib import admin
from import_export import resources
from import_export.admin import ImportMixin
from .models import Book,BookPrice,Publisher

class PublisherAdmin(ImportMixin,admin.ModelAdmin):
    class PublisherResource(resources.ModelResource):
        class Meta:
            model = Publisher
            fields = ("id","name",)
    resource_class = PublisherResource

class BookAdmin(ImportMixin,admin.ModelAdmin):
    class BookResource(resources.ModelResource):
        class Meta:
            model = Book
            fields = ("id","title","publisher",)
    resource_class = BookResource


class BookPriceAdmin(ImportMixin,admin.ModelAdmin):
    class BookPriceResource(resources.ModelResource):
        class Meta:
            model = BookPrice
            fields = ("id","book","price",)
    resource_class = BookPriceResource



admin.site.register(Publisher,PublisherAdmin)
admin.site.register(Book,BookAdmin)
admin.site.register(BookPrice,BookPriceAdmin)

** Set the details of the csv file to be imported with Resource. Describe the column to be read in fields. In addition to fields, there are import_id_fields etc., and you can specify the id as a command. In addition, you can change the format that accepts only csv, and whether to import even if there is a change in the data.

csv file creation

From here, create three csv files in Excel. When pointing to other models as shown below, it is possible to link by writing the id number to link. For example, Book's "Kimi no Koe" and "Kimi no Oto" refer to objects from the publisher Nakagawa Bunko. Since the Book of BookPrice has a one-to-one relationship, the id number of BookPrice and book are the same.

Model: Publisher

スクリーンショット 2020-04-24 18.10.46.png

Model: Book

スクリーンショット 2020-04-24 18.27.56.png

Model: BookPrice

スクリーンショット 2020-04-24 18.41.27.png

Try to import

python manage.py createsuperuser #I've been asked a lot, but as it's written
python manage.py runserver

If you go to the django management screen, you'll see the three models you created earlier on the management screen. If you click Publisher in this list, the following screen will be displayed. スクリーンショット 2020-04-24 18.48.40.png IMPORT is displayed in the upper right corner of this screen, so import from here. Then, the model you created will be registered. It is also linked with the model, so please check it.

At the end

In this article, I used django-import-export to register the data on the django model. Specifically, we created three models, each of which has a corresponding relationship with the other model, but showed that the data relationship can be maintained by specifying their ids. Next, if I have a chance, I would like to try creating a many-to-many model.

Recommended Posts

Try using django-import-export to add csv data to django
Try using Django templates.html
Add CSV export function to management screen with django-import-export
(Python) Try to develop a web application using Django
How to add new data (lines and plots) using matplotlib
Handle csv files with Django (django-import-export)
Try using pynag to configure Nagios
Parsing CSV format data using SQL
Try to put data in MongoDB
Try to get statistics using e-Stat
[Django] Command to output QuerySet to csv
Development of WEB application using Django [Add data from management screen]
Try to analyze online family mahjong using Python (PART 1: Take DATA)
[Django 2.2] Add a New badge to new posts with a date using a template filter
Try using scanpy's data integration function (sc.tl.ingest)
Try to detect fusion movement using AnyMotion
Try using the Python web framework Django (1)-From installation to server startup
Try to get the road surface condition using big data of road surface management
Try to operate Excel using Python (Xlwings)
Try converting to tidy data with pandas
I tried to create CSV upload, data processing, download function with Django
Try to download Youtube videos using Pytube
Convert csv, tsv data to matrix with python --using MovieLens as an example
Try to create a Todo management site using WebSocket with Django (Swamp Dragon)
How to create sample CSV data with hypothesis
[Introduction to SEIR model] Try fitting COVID-19 data ♬
Dynamically add fields to Form objects in Django
Try to aggregate doujin music data with pandas
Try to separate Controllers using Blueprint in Flask
Just add the python array to the json data
I want to visualize csv files using Vega-Lite!
[Django] How to get data by specifying SQL.
Preparing to try "Data Science 100 Knock (Structured Data Processing)"
How to get article data using Qiita API
Write CSV data to AWS-S3 with AWS-Lambda + Python
Try to create an HTTP server using Node.js
How to search HTML data using Beautiful Soup
Convert json format data to txt (using yolo)
Add functionality to modify Django day shift information
Try using Tkinter
Try using docker-py
Try using cookiecutter
Try using PDFMiner
Every time I try to read a csv file using pandas, I get a numpy error.
Try using geopandas
Try using Selenium
Try using scipy
Try using pandas.DataFrame
Try using django-swiftbrowser
Try using matplotlib
Try using tf.metrics
Try using PyODE
Create a shogi game record management application using Django 5 ~ Pass DB data to Template ~
I convert AWS JSON data to CSV like this
100 language processing knock-92 (using Gensim): application to analogy data
[Python] [Django] How to use ChoiceField and how to add options
Python beginners try adding basic auth to Django admin
Try using COVID-19's open data from Yokohama / Tokyo / Osaka
Collectively register data in Firestore using csv file in Python
How to scrape horse racing data using pandas read_html
Try to make RESTful API with MVC using Flask 1.0.2