- What is Django
- Django Download & Install
- Django Start Project
- Django Skeleton Project
- Django Models
- Django Import Export
- Django Admin
- Django Urls & Views
- Django Queryset
- Django Forms
- Django Templates
- Django Auth
- Django Session
- Django Cookies
- Django Pagination
- Django Email Sending
- Django Logging
- Django Deployment
- Django Rest Framework
Django Import Export Library
django-import-export library provides an import_export app for your django project.
You can use the import_export app with Django Admin. It will simpy add fully functional import and export buttons to admin dashboard.
Model data can be imported from csv, xml, excel, json etc. You may also export data to multiple file formats like csv, xls, xlsx, tsv, ods, json, yaml and html.
You may easily add the import export app in 4 easy steps:
- Step 1: Install django-import-export via pip
- Step 2: Add import_export to INSTALLED_APPS in settings.py
- Step 3: Import and inherit ImportExportModelAdmin in admin.py
- Step 4: Set import_export app permissions under settings.py (if needed)
Installing django-import-export library
To install django-import-export library simply run:
Add import_export app to the project settings.py
Now, you must add import_export app in your project settings.py under the INSTALLED_APPS attribute list. Notice that the "-" is replaced with "_" because of Python's nomenclature rules.
INSTALLED_APPS = [ ... 'import_export', ]
Sample Project of Django Import Export
[Note: Complete project is at the end of the page for free download.]
We are going to use an example model class called Blog under myapp
from django.db import models class Blog(models.Model): title = models.CharField(max_length=120) author = models.CharField(max_length=120) date_of_publishing = models.DateField(auto_now_add=True) content = models.TextField() def __str__(self): return self.title
Now, simply import and inherit ImportExportModelAdmin class in admin.py
from import_export.admin import ImportExportModelAdmin from django.contrib import admin from .models import Blog class BlogAdmin(ImportExportModelAdmin, admin.ModelAdmin): ... admin.site.register(Blog, BlogAdmin)
Note: ImportExportModelAdmin should be prior to admin.ModelAdmin in the multiple inheritance sequence to avoid MRO (Method Resolution Order) error.
Django import_export app permissions
With above code all staff users will have access to django import export feature under Admin interface. In order to restrict access to user with add, view, change or delete permissions, add the code below in project settings.py.
In place of delete you may use other permissions like "add", "view" or "change".