circuit

How to Import or Export Data from Django Admin


In this article, I am going to show you a very easy way to add import/export functionality to Django Admin.

For those creating web applications with Python/Django, you are probably using Django Admin to manage your models.

Sometimes a need might arise to export data for further analysis or processing, to move it to another environment, or just to backup.

Also, for moving to another environment the import functionality is a bonus in helping to move this data.

In this article, I assume that you know how to create a Django web application, create models, and add them to the Django Admin.

The focus will be only on the import/export functionality.

Django Admin Import/Export Library

To add this capability to Django Admin, I will use the following library:

GitHub - django-import-export/django-import-export

You can find the documentation for this library at ReadTheDocs.

Some of the main features are:

  • Support multiple formats (Excel, CSV, JSON, … and everything else that tablib supports)
  • Django Admin integration for importing
  • Preview import changes
  • Admin integration for exporting
  • Export data respecting admin filters

Installing Django Admin Import/Export

To install the library we use the standard pip installation method:

pip install django-import-export

In case you just want to use the library in your other Django applications there is no other configuration needed.

In case you want to use it also in the Django Admin interface, we need to add the new application to the Django settings:

# settings.py
INSTALLED_APPS = (
    ...
    'import_export',
)

You should also collect the static to ensure proper rendering:

python manage.py collectstatic

From now on we can use the library to create resources to import/export data from your applications. You can read more here.

The focus of this article is the functionality for Django Admin so I will not cover these topics. Let me know if you would like a follow-up article on this.

Configuring Models Import/Export for Django Admin

Let's take an example of a Task model that we define in our application:

# models.py
from django.db import models
class Task(models.Model):
    name = models.CharField(max_length=256)
    description = models.CharField(max_length=256)
    due_date = models.DateTimeField()
    is_complete = models.BooleanField(default=False)

Normally we would add this model to the admin.py file to be able to access it from the Django Admin.

For the import/export library, we do the same with one added definition to enable the functionality:

# admin.py
from django.contrib import admin
from import_export.admin import ImportExportMixin
from .models import Task
class TaskAdmin(ImportExportMixin, admin.ModelAdmin):
    list_display = ['name', 'description', 'due_date', 'is_complete']


admin.site.register(Task, TaskAdmin)

We create the class TaskAdmin where we reference the ImportExportMixin, which adds the Import and Export buttons to our model view in the Django Admin.

We also add the list of fields that we want to show in the Django Admin.

That is all that we need to set up to use the library in Django Admin, of course, there are other configuration options and custom options to Export and Import, but for this article, the goal is to show you how easy it is to add the standard functionality.

You can read the documentation for more details.

Importing and Exporting Data from Django Admin

Let's see a practical example of the new functionality in action.

Access the Django Admin and our Task model screen and we will now see 2 new buttons for importing and exporting:

If we click on the Export button we are presented with the options for exporting, mainly to which format we would like the export file to save as:

Checking the file, let's take the example of exporting to CSV, we see the exported data:

We can also import this data and preview what changes are going to be done before importing. Starting by selecting a file:

In the next example, I removed the id field contents in the CSV file, and this means that the import function will now create a new record. If the id matches the existing id's then those records would be updated:

Let's import the file and make the changes. We now will have 2 Task records:

And that is all that is needed to have Import and Export functionalities in the Django Admin application.

All we did was install a new application/library and define the Import/Export methods in the admin class definition for the model.

You can find all the code in the GitHub repository:

GitHub - nunombispo/DjangoImportExport: Django Admin Import/Export

Conclusion

The ability to move data between environments or just export the data as a backup or for further processing is a plus in today's world where data is king.

Django Admin is a great application to manage the data models in a Django application but (for now at least) it lacks this functionality.

But we can easily add that functionality with this excellent library.




Continue Learning