Prerequisites for the project are 

Python 3.x -
Django and django-import-export, they both can be installed by

pip install django
pip install django-import-export

Let's start by creating a sample project

django-admin startproject tutorial


And then move into the project folder.

This will set us up with all the required files and settings for our project.

We can check for installation by running python runserver and then visit the URL by using any browser you must be able to see this

Welcome Page

Now let us add an app to our project where we will be writing our code for this we are going to the file which is already present in our project folder.

python startapp myapp

Let us register this app in our file so Django can pick it up.


Now I will demonstrate how to export or import data from Django by building a sample application, We will not write any GUI for this but instead, we will use the Django's admin panel. So let us get started by creating a model. We will do this in myapp/


from django.db import models

# Create your models here.

class Person(models.Model):
    first_name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=100)
    country = models.CharField(max_length=100)


Now run migrations so Django can update the database in our case this is sqlite3. You can change this any other database if you want to, but for now, we will stick with the default.

Before we go check out the admin, Let us register our Person model in the admin panel to do that, we can add the following in our in myapp folder. (myapp/

from django.contrib import admin

from .models import Person
# Register your models here.


Now to use Django admin panel we need an admin account, Well that’s easy to create, just run python createsuperuser in your console

(mywebsite) [email protected]:~/Desktop/tutorial$ python createsuperuser
Username (leave blank to use 'vineeth'): vineeth
Email address: [email protected]
Password (again): 
Superuser created successfully.


Your model should appear here in the admin page.

Person Model

Now we got the app setup, let us add the import/export functionality this can be easily done by using 3rd party app django-import-export.
Let us start by adding that into our INSTALLED_APPS list, Do not forget to install the package by pip install django-import-export.


Once that’s done, It easy to add this to our models, Update myapp/ to this.


from django.contrib import admin

from .models import Person

from  import_export.admin import ImportExportModelAdmin
from import_export import resources

# Create a resource class
class PersonResource(resources.ModelResource):
    class Meta:
        model = Person

# Add resource class to our admin
class PersonAdmin(ImportExportModelAdmin):
    resource_class = PersonResource

# Register your models here., PersonAdmin)


Now you must be able to see these 2 new buttons in

Now let us test our project by adding a few people into our models

Now click on export, Select the type you want to export as and that’s it! You can integrate this into any type of model class in Django!

And there we go,  we've successfully downloaded(exported) our model data from django!

blog comments powered by Disqus