Importing and exporting data with admin integration.
Django-import-export is a Django application and library for importing and exporting data with included admin integration.
- Published on
4 min read•
Must Know Django Packages
django-import-export is a Django application and library for importing and exporting data with included admin integration.
Latest version :
django-import-export release v3 is currently in beta (pypi)
- support multiple formats (Excel, CSV, JSON, etc) read more from official docs
- admin integration for importing/exporting
- preview import changes
- export data respecting admin filters
pip install django-import-export
specific version can be installed by specifying the version.
pip install django-import-export==3.0.0b4
Add the package to
# settings.py INSTALLED_APPS = ( ... 'import_export', )
Collect statics to support styling in django admin panel.
python manage.py collectstatic
Integrating to admin panel
We need to define a Resources class that refers to a model in which we want to enable import export.
# admin.py from import_export import resources class BlogResource(resources.ModelResource): class Meta: model = Blog
Now we can register the model to admin with admin.register
#admin.py from import_export.admin import ImportExportModelAdmin class BlogAdmin(ImportExportModelAdmin): resource_class = BlogResource admin.site.register(Blog, BlogAdmin)
Full code looks like this
from django.contrib import admin from apps.blog.models import Blog from import_export import resources from import_export.admin import ImportExportModelAdmin # Register your models here. class BlogResource(resources.ModelResource): class Meta: model = Blog class BlogAdmin(ImportExportModelAdmin): resource_class = BlogResource admin.site.register(Blog, BlogAdmin)
That's it. Here are some screenshot to verify our configuration.
Here we can see that we can export on various formats. Similarly we can import on various formats. Furthermore we can use the import and export functionality programmatically as well.
Let's see an example to export using the shell. Fire up the shell by
python3 manage.py shell
We can do this by reusing the
BlogResource that we added on
from apps.blog.admin import BlogResource dataset = BlogResource().export() print(dataset.csv)
This will print the exported data in csv format
id,author,poster,title,caption,slug,tags,markdown_body,series,views,is_archived,is_featured,created_at,updated_at 3,1,,sdfsd,sdfsdfsd,sdfs,1,sdfsd,,60,0,0,2022-08-20 10:05:19,2022-08-27 11:18:08 1,1,uploads/299496744_5438537262880214_6238834739525833684_n.jpeg,Title 1,some good caption,good-caption,1,"# THis is heading ## subheading body text[!(http://localhost:8000/media/editor/IMG_20180219_152316_20220825111743599583.jpeg)](http://localhost:8000) some `code` hightlight checking",,225,0,0,2022-08-20 10:04:48,2022-08-26 20:50:41 2,1,uploads/1660795538413.jpeg,Tittle 2,Tittle 2,sss,1,sdfsdf,,4,0,0,2022-08-20 10:05:08,2022-08-25 21:47:41
That's a wrap
Please do follow the official docs for more information. Thank you for reading this article.
Any feedback/comment/suggestions to improve the articles are welcomed!