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.
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.
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
.
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
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 inherits
ModelResource 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.
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. .. ..
https://qiita.com/kira_puka/items/14a1a604a428a4c68884 https://django-import-export.readthedocs.io/en/stable/index.html
Recommended Posts