Skip to content

maisim/django-import-export-extensions

 
 

Repository files navigation

django-import-export-extensions

Build status on Github Test coverage Supported python versions Supported django versions Documentation Status Downloading statistic

Description

django-import-export-extensions extends the functionality of django-import-export adding the following features:

  • Import/export resources in the background via Celery
  • Manage import/export jobs via Django Admin
  • DRF integration that allows to work with import/export jobs via API
  • Support drf-spectacular generated API schema
  • Additional fields and widgets (FileWidget, IntermediateManyToManyWidget, IntermediateManyToManyField)

Installation

To install django-import-export-extensions, run this command in your terminal:

$ pip install django-import-export-extensions

Add import_export and import_export_extensions to INSTALLED_APPS

# settings.py
INSTALLED_APPS = (
    ...
    "import_export",
    "import_export_extensions",
)

Run migrate command to create ImportJob/ExportJob models and collectstatic to let Django collect package static files to use in the admin.

$ python manage.py migrate
$ python manage.py collectstatic

Usage

Prepare resource for your model

# apps/books/resources.py
from import_export_extensions.resources import CeleryModelResource

from .. import models


class BookResource(CeleryModelResource):

    class Meta:
        model = models.Book

Use CeleryImportExportMixin class and set resource_class in admin model to import/export via Django Admin

# apps/books/admin.py
from django.contrib import admin

from import_export_extensions.admin import CeleryImportExportMixin

from .. import resources


@admin.register(models.Book)
class BookAdmin(CeleryImportExportMixin, admin.ModelAdmin):
    resource_class = resources.BookResource

Prepare view sets to import/export via API

# apps/books/api/views.py
from .. import resources

from import_export_extensions.api import views


class BookExportViewSet(views.ExportJobViewSet):
    resource_class = resources.BookResource


class BookImportViewSet(views.ImportJobViewSet):
    resource_class = resources.BookResource

If you use multiple or custom admin site, register required models to them

# app/admin.py
from import_export_extensions.models import ExportJob
from import_export_extensions.admin.model_admins.export_job_admin import ExportJobAdmin

from django.apps import AppConfig

custom_admin_site = BaseAdminSite(name="custom_admin")
# ...
cutom_admin_site.register(ExportJob, ExportJobAdmin)

Don't forget to configure Celery if you want to run import/export in background

Links:

License:

  • Free software: MIT license

About

django-import-export-extensions

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 92.6%
  • HTML 6.2%
  • Other 1.2%