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
・ 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
Please install the following packages.
pip install django django-import-export
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
]
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
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.
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.
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. 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.
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