Django Import Export Excel to Database Example
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 exampleStep 2 : Create a App
python3 manage.py startapp coreStep 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-exportStep 4 : Update setting.py
In this step we require to do two things in our settings.py file add the below lines to your settings.py file:
Next, you need to add it in the settings.py file as follows:
.... INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'core', 'import_export', ]Step 5: Create a Model
In this step we will To represent our model in the admin interface, add this line in models.py add the following code:core/models.py
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.titleStep 6 : Update the admin.py
In this step, we need to create the views for performing fetch record to the database.Open the core/admin.py file and add:core/admin.py
from import_export.admin import ImportExportModelAdmin from django.contrib import admin from .models import Post class PostAdmin(ImportExportModelAdmin, admin.ModelAdmin): ... admin.site.register(Post, 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 settings.py.settings.py
IMPORT_EXPORT_IMPORT_PERMISSION_CODE = 'delete' IMPORT_EXPORT_EXPORT_PERMISSION_CODE = 'delete'
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 manage.py runserver
Next, go to the http://localhost:8000/admin address with a web browser.Import:
You can download this demo csv file.. Download Demo FileExport:
I Hope It will help you....