If you have been using Django admin, you might have realized that there is no way that you can import to export some data from the Django admin panel in supported formats such as
JSON or an Excel Spreadsheet etc.
Well, without wasting any time, let’s directly jump into the implementation of how to add import-export functionality in Django Admin for any model.
We will use a popular third-party library called
django-import-export in this blog, you can find this library on PyPI.
The simplest way to install
django-import-export is to use
pip install django-import-export
After the successful installation, you just need to add
import_export package in your
INSTALLED_APPS list of
# settings.py ... INSTALLED_APPS = [ ... 'import_export', ] ...
Integration with Django Admin:
django-import-export comes with multiple admin
mixins to make it easy for you to integrate with any Django model.
If you want to integrate both Import and Export together in a model admin, you can import the
ImportExportModelAdmin and register your model admin, here is the code:
# app_name/admin.py from app_name.models import Book from import_export.admin import ImportExportModelAdmin class BookAdmin(ImportExportModelAdmin): ...
If you are looking to only integrate the Import or maybe only the export feature in your model admin, you can use the
ExportMixin respectively. But keep in mind that these
mixins are combined with
For only Import:
# app_name/admin.py from django.contrib import admin from app_name.models import Book from import_export.admin import ImportMixin class BookAdmin(ImportMixin, admin.ModelAdmin): ...
For only Export:
# app_name/admin.py from django.contrib import admin from app_name.models import Book from import_export.admin import ExportMixin class BookAdmin(ExportMixin, admin.ModelAdmin): ...
Any thoughts? Write it down in the comments.
For more such crispy blogs daily, follow Dev.Junction, subscribe to our newsletter and get notified.