Prerequisites for the project are
Python 3.x - https://www.python.org/downloads/
Django and django-import-export, they both can be installed by
pip install django pip install django-import-export
Let's start by creating a sample project
django-admin startproject tutorial
And then move into the project folder.
This will set us up with all the required files and settings for our project.
We can check for installation by running python manage.py runserver and then visit the URL by using any browser you must be able to see this
Now let us add an app to our project where we will be writing our code for this we are going to the manage.py file which is already present in our project folder.
python manage.py startapp myapp
Let us register this app in our settings.py file so Django can pick it up.
INSTALLED_APPS = [ 'myapp', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', ]
Now I will demonstrate how to export or import data from Django by building a sample application, We will not write any GUI for this but instead, we will use the Django's admin panel. So let us get started by creating a model. We will do this in myapp/models.py
from django.db import models # Create your models here. class Person(models.Model): first_name = models.CharField(max_length=100) last_name = models.CharField(max_length=100) country = models.CharField(max_length=100)
Now run migrations so Django can update the database in our case this is sqlite3. You can change this any other database if you want to, but for now, we will stick with the default.
Before we go check out the admin, Let us register our Person model in the admin panel to do that, we can add the following in our admin.py in myapp folder. (myapp/admin.py)
from django.contrib import admin from .models import Person # Register your models here. admin.site.register(Person)
Now to use Django admin panel we need an admin account, Well that’s easy to create, just run python manage.py createsuperuser in your console
(mywebsite) [email protected]:~/Desktop/tutorial$ python manage.py createsuperuser Username (leave blank to use 'vineeth'): vineeth Email address: [email protected] Password: Password (again): Superuser created successfully.
Your model should appear here in the admin page.
Now we got the app setup, let us add the import/export functionality this can be easily done by using 3rd party app django-import-export.
Let us start by adding that into our INSTALLED_APPS list, Do not forget to install the package by pip install django-import-export.
INSTALLED_APPS = [ 'myapp', 'import_export', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', ]
Once that’s done, It easy to add this to our models, Update myapp/admin.py to this.
from django.contrib import admin from .models import Person from import_export.admin import ImportExportModelAdmin from import_export import resources # Create a resource class class PersonResource(resources.ModelResource): class Meta: model = Person # Add resource class to our admin class PersonAdmin(ImportExportModelAdmin): resource_class = PersonResource # Register your models here. admin.site.register(Person, PersonAdmin)
Now you must be able to see these 2 new buttons in 127.0.0.1:8000/admin
Now let us test our project by adding a few people into our models
Now click on export, Select the type you want to export as and that’s it! You can integrate this into any type of model class in Django!
And there we go, we've successfully downloaded(exported) our model data from django!