Django Import Export Excel to Database Example

Published On: 16/07/2022 | Category: Django Python

Hi Dev,

In this example, I will show you django import export excel to database example. I would like to share with you django import excel file. let’s discuss about django import export excel to database example csv. This article will give you simple example of django import export excel to database example download. You just need to some step to done django import export excel to database example in python.

The django-import-export library supports multiple formats, including xls, csv, json, yaml, and all other formats supported by tablib. It also have a Django admin integration, which is really convenient to use.

Here i explained simply step by step example of django import export excel to database.

Step 1 : Create a Project

In this step, we’ll create a new django project using the django-admin. Head back to your command-line interface and run the following command:

django-admin startproject example
Step 2 : Create a App
python3 startapp core
Step 3 : Installing Django-Import-Export Library

First of all we need to install django-import-export library simply run below command.

pip install django-import-export
Step 4 : Update

In this step we require to do two things in our file add the below lines to your file:

Next, you need to add it in the file as follows:

Step 5: Create a Model

In this step we will To represent our model in the admin interface, add this line in add the following code:

from django.db import models

# Create your models here.

class Post(models.Model):
    title = models.CharField(max_length=255)
    content = models.TextField()

    def __str__(self):
        return self.title
Step 6 : Update the

In this step, we need to create the views for performing fetch record to the database.Open the core/ file and add:

from import_export.admin import ImportExportModelAdmin
from django.contrib import admin
from .models import Post

class PostAdmin(ImportExportModelAdmin, admin.ModelAdmin):
        ..., PostAdmin)  
Django Import_export App Permissions

Here, one thing with the above code, all staff users will have access to django import export feature under the Admin interface. In order to restrict access to users with add, view, change or delete permissions, add the code below in project

So, in place of delete, you may use other permissions like "add", "view" or "change".

Run the Server

In this step, we’ll run the local development server for playing with our app without deploying it to the web.

python runserver

Next, go to the http://localhost:8000/admin address with a web browser.


You can download this demo csv file.. Download Demo File


I Hope It will help you....