Migrate from original django-import-export package
If you are already using django-import-export
and want to use django-import-export-extensions
,
you can easily switch to it and get the benefits of background import/exporting.
First of all, install the package according to the instruction.
And now, all you need is to change base classes of resources and admin models.
Migrate resources
To use resources that provides import/export via Celery just change base resource classes from
original package to CeleryResource
/ CeleryModelResource
from django-import-export-extensions
:
- from import_export import resources
+ from import_export_extensions import resources
class SimpleResource(
- resources.Resource,
+ resources.CeleryResource,
):
"""Simple resource."""
class BookResource(
- resources.ModelResource,
+ resources.CeleryModelResource,
):
"""Resource class for `Book` model."""
class Meta:
model = Book
Migrate admin models
Then you also need to change admin mixins to use celery import/export via Django Admin:
from django.contrib import admin
- from import_export.admin import ImportExportModelAdmin
+ from import_export_extensions.admin import CeleryImportExportMixin
from . import resources
class BookAdmin(
- ImportExportModelAdmin,
+ CeleryImportExportMixin,
+ admin.ModelAdmin,
):
"""Resource class for `Book` model."""
resource_classes = (
resources.BookResource,
)
If you only need import (or export) functionality, you can use CeleryImportAdminMixin
(CeleryExportAdminMixin
) instead of CeleryImportExportMixin
.
Migrate custom import/export
Background import/export is implemented based on ImportJob
/ExportJob
models. So simple
resource.export()
won’t trigger a celery task, it works exactly the same as the original
Resource.export()
method. To start background import/export, you need to create objects of
import/export job:
1>>> from .resources import BandResource
2>>> from import_export.formats import base_formats
3>>> from import_export_extensions.models import ExportJob
4>>> file_format = base_formats.CSV
5>>> file_format_path = f"{file_format.__module__}.{file_format.__name__}"
6>>> export_job = ExportJob.objects.create(
7 resource_path=BandResource.class_path,
8 file_format_path=file_format_path
9 )
10>>> export_job.export_status
11'CREATED'
Using the export_status
(import_status
) property of the model, you can check the current status of the job.
There is also a progress
property that returns information about the total number and number of completed rows.
1>>> export_job.refresh_from_db()
2>>> export_job.export_status
3'EXPORTING'
4>>> export_job.progress
5{'state': 'EXPORTING', 'info': {'current': 53, 'total': 100}}
6>>> export_job.refresh_from_db()
7>>> export_job.export_status
8'EXPORTED'
9>>> export_job.data_file.path
10'../media/import_export_extensions/export/3dfb7510-5593-4dc6-9d7d-bbd907cd3eb6/Artists-2020-02-22.csv'