Django Custom Import Export example

Published On: 25/03/2025 | Category: Django
Django Custom Import Export Excel CSV Tutorial

Hi Dev,

Welcome to this comprehensive Django Custom Import Export tutorial. In this guide, you'll learn how to import and export Excel or CSV files in Django using the powerful django-import-export library. This tutorial walks you through step-by-step implementation, covering models, views, templates, and admin integration.

The django-import-export library is a handy tool that supports a variety of formats such as XLS, CSV, JSON, YAML, and more. Its seamless integration with Django Admin makes data transfer effortless and efficient.

Let’s dive into the step-by-step setup and learn how to build Django import export features for CSV and Excel files.

Step 1: Create a Project

django-admin startproject example

Step 2: Create a App

python3 manage.py startapp core

Step 3: Installing Django-Import-Export Library

pip install django-import-export

Step 4: Update setting.py

INSTALLED_APPS = [
    ...
    'import_export',
    'core',
]

IMPORT_EXPORT_USE_TRANSACTIONS = True

Step 5: Create Model and Resources

core/models.py
class Employee(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=60)
    email = models.EmailField(blank=True)
    day_started = models.DateField()
    location = models.CharField(max_length=100, blank=True)

    def __str__(self):
        return self.first_name
python manage.py makemigrations
python manage.py migrate
core/resources.py
from import_export import resources
from .models import Employee

class EmployeeResource(resources.ModelResource):
    class Meta:
        model = Employee

Step 6: Creating the Views

core/views.py
from django.shortcuts import render
from django.http import HttpResponse
from tablib import Dataset
from .resources import EmployeeResource
from .models import Employee

def export_data(request):
    ...
    return render(request, 'export.html', {'employees': Employee.objects.all()})

def import_data(request):
    ...
    return render(request, 'import.html')    

Step 7: Creating Templates

core/import.html core/export.html

Step 8: Import Data

first_name,last_name,email,day_started,location
Bhavesh,Sonagra,sonagrabd@gmail.com,2022-08-15,Rajkot
Nik,Patel,nik@gmail.com,2022-07-05,Rajkot
core/admin.py
from import_export.admin import ImportExportModelAdmin
from django.contrib import admin
from .models import Employee

@admin.register(Employee)
class EmployeeAdmin(ImportExportModelAdmin):
    pass

Step 9: Creating URLs

core/urls.py
from django.urls import path
from . import views

urlpatterns = [
    path('export', views.export_data, name='export_data'),
    path('import', views.import_data, name='import_data'),
]
example/urls.py
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('core.urls')),
]

Run the Server

python manage.py runserver
# Access these URLs:
http://localhost:8000/import
http://localhost:8000/export

Import Page Preview:

Django Import Page Example

Export Page Preview:

Django Export Page Example

I hope this tutorial on Django Custom Import Export Excel and CSV Files helps you integrate it easily into your application. Now you can manage bulk data faster, with admin integration and custom views support.

Happy Coding and keep exploring Django!


🔍 FAQs - Django Import Export

Q1: What is django-import-export?

It’s a Django package that allows importing and exporting data in multiple formats like Excel, CSV, JSON, and more using simple admin or view configurations.

Q2: Can I use it with Django Admin?

Yes, it integrates perfectly with Django Admin using the ImportExportModelAdmin class.

Q3: What file formats are supported?

It supports CSV, XLS, XLSX, JSON, YAML, and more — all powered by the Tablib library.

Q4: How can I validate data before import?

You can use the dry_run=True flag which checks for errors before saving to the database.

Q5: Do I need to create templates?

No, if you're using admin. But for custom views, you’ll need templates like `import.html` and `export.html`.