Django 3: Set a Model Import/Export in /admin
This article is to share how I set the buttons Import and Export in Django Admin — Py:3.7.9 | Win10

This is the way I did on the way that I know/learned and it worked for me. If you can improve this article, let me know! The objective is get better and better!!! 😃😃😃 … And, in advanced, sorry for my English… I’m from Brazil and if you see something wrong, give me tips! 😁
Looking for options to populate database with start data using Django OMR to persist in DB, I found this link in stackoverflow and the user wobbily_col post a fantastic tip! So I knew the package django-import-export. After you finish this article, you will :
- be a little better in Django and dev, man!
- Create Resources in Django Admin
- One of the best “Importer” and “Exporter” in Django Admin, congrats!
- share what you learned with others! That’s the magic! You receive, you give!
First of all: Modules and versions considered in this example
This example consider that you have installed Python, Django, dependencies, migrate models and config admin.
Windows 10 PRO
Python : 3.7.9
pip: 20.2.4
Django==3.1.2
django-import-export==2.4.0
- And all dependencies module to Django and OMR
1°: What we will do…
We will create the possibility to import and export files in Django Admin to get fast upload a big amount of data.
Install package django-import-export
On your Python environment execute the command below:
pip install django-import-export==2.4.0
This command install in version 2.4.0, used in this example.
Step 2: Add django-import-export
to INSTALLED_APPS
After install, add your module on INSTALLED_APPS on settings.py of your Django project

In my projects, I set many different CONSTANT to separate Django Core apps, Third Parties apps and the apps of my projects and the I concatenated them! Organize…
Step 3: Class Resource to your ModelAdmin
In the app dir, create a file resources.py

In your file resources, create the resource to your model:
- Import the modules below:

— fields
and ForeignKeyWidget:
to set the field of related Model (You’ll see example below)
— Region
and State
: State
is the Model that we will import and Region
is the Related Model to State
— resources
: to State
inherits ModelResource from module resource
of package import_export
- Set field from related Model and class
Meta
to your setModel
and options of resources. You can see all options in readthedocs. Below I set:

->region
: Is the related field on State
to Model Region
, you set which field you set on file to import, in this example I set the field name
of Model Region
. And on file to import I fill on column region
the name of region
instead id
->model
: to set the Model that I want Import/Export
->skip_unchanged
: to skip objects that don’t have changes and not update them->reported_skip
: To show in log in the end of execution the objects that was skipped. To see manually and check a sampling if really don’t have changes… Yeah, I’m worried about that!->fields
: the fields that you want import, as you see, the field of related Model you set above. In this case, id
field was set because I’ll import a specific id
, but if you don’t set id
in fields
, will save id
as you set in your Model
, or as Model
default.
Step 4: Set your ModelAdmin
In your ModelAdmin
(In this example StateAdmin
):
- Import module
ImportExportModelAdmin
- Import the resource that you create in step 3
- set the property
resource_class
to your Resource that you created - Don’t forget to register in your
Model
andModelAdmin
to you see in your/admin

Step 5: Importing datas to database.
In this step you will access your admin page http://project_url/admin
and enter in your Model
home page. In this example, http://project_url/admin/locals/state/
(locals
is App Name) and you will see the buttons to Import
and Export.

http://project_url/admin/locals/state/
Clicking in Import
and you’ll redirect to State page import.

Will show the fields that you have to do upload, in this example I used and selected .xlsx
, but you can choose other options. And so you click in Submit
button to start your import.
After you click in submit, if the file is OK, will redirect to page that show a table with all rows of file and to confirm if you want import. As below:

When you click in CONFIRM IMPORT
button, will redirect to page of Model flashing the status of import.

Export
To export is just click in EXPORT
button and choose the file format that you want. It’s easy, simple and useful!!!

Finally
Congratulations, now you are a “Importer” and “Exporter” in Django Admin! 😆
I hope that was useful for you! Thanks for read!
Some usefull links
Import Data using django import-export library — Posted by Vinaykumarmaurya, Fantastics Tips!
Tutorial #8 — Registering Models and Import-Export in Django Administration. — Posted by SRP Blogs
Import Data using django import-export library