This article is based on the admin background that comes with Django to import Excel, csv, Json and other format files and update the background database.
The core is to introduce django-import-export
modules.
1. Test phase data preparation:
Let’s create an app first: app01
python manage.py startapp app01
Then write the following model data in models.py under the app01 folder:
# app01/models.py
class Author(models.Model):
name = models.CharField(max_length=100)
def __str__(self):
return self.name
class Category(models.Model):
name = models.CharField(max_length=100)
def __str__(self):
return self.name
class Book(models.Model):
name = models.CharField('Book name', max_length=100)
author = models.ForeignKey(Author, blank=True, null=True,on_delete=models.CASCADE)
author_email = models.EmailField('Author email', max_length=75, blank=True)
imported = models.BooleanField(default=False)
published = models.DateField('Published', blank=True, null=True)
price = models.DecimalField(max_digits=10, decimal_places=2, null=True, blank=True)
categories = models.ManyToManyField(Category, blank=True)
def __str__(self):
return self.name
2. Installationdjango-import-export
Specifying the Tsinghua mirror will be much faster
pip install django-import-export -i https://pypi.tuna.tsinghua.edu.cn/simple
3. settings.py
Modify and add configuration items
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app01',
'import_export',
]
# django-import-export配置
IMPORT_EXPORT_USE_TRANSACTIONS = True # 默认值为False。它确定库是否会在数据导入中使用数据库事务,以确保安全。
Two modifications:
- Register import_export,app01
- Add configuration item: IMPORT_EXPORT_USE_TRANSACTIONS = True
4. Add information displayed and exported by admin
To ultimately use import_export
to implement the import and export function, you need to admin.py
specify the model and related field information that need to be imported and exported in .
for example:
#app01/admin.py
from import_export import resources
from .models import Book
from import_export.admin import ImportExportModelAdmin
class BookResource(resources.ModelResource):
class Meta:
model = Book
class BookAdmin(ImportExportModelAdmin):
resource_classes = [BookResource]
admin.site.register(Book, BookAdmin)
The above code is implemented as follows:
class BookResource(resources.ModelResource):
Specify the fields and methods related to model import and export (here simply pass:model = Book
, specifying that import and export will involve all fields)
class BookAdmin(ImportExportModelAdmin):
Inherited fromImportExportModelAdmin
Implementadmin
Integration.
admin.site.register(Book, BookAdmin)
: Register the model to the Django backend.
5. Test demonstration
admin homepage:
On the Book model management page, the import and export buttons appear.
Prepare the file and import the data (the file header needs to be named according to the field name, otherwise the imported data will not be recognized)
6. Conclusion
In order to quickly implement the function above, we wrote resources
the and admin
class in a file. In the actual project, all the files defining Resource can be written into independent files resources.py
and imported when used.
class BookResource(resources.ModelResource):
class Meta:
model = Book