Importing data into models in Django

In this article, we are going to see how to import data from json format to model. We can import data from json, csv, xlsx, yml, etc. to model.

First of all, create a Django project and an app. Set up urls and do some basic stuff like adding the app in INSTALLED_APPS.

Create a model. Here, we don't have much to do with, or any html file. We only have to work with,, and admin urlpoint.


Install the django-import-export package −

pip install django-import-export

In, add the following line −

INSTALLED_APPS += ['import_export']

It will add import_export as an app in our project.

Create a model −

class StudentData(models.Model):

We created the model for testing and trying.


from django.contrib import admin
from .models import StudentData
from import_export import resources
from import_export.admin import ImportExportModelAdmin

class StudentResource(resources.ModelResource):
   class Meta:
      model = StudentData
class StudentAdmin(ImportExportModelAdmin):
   resource_class = StudentResource,StudentAdmin)

Here we created a model resource for import and export. Then, we created an admin and registered it.

File format for JSON should be like this −

      "id": 13,
      "name": "John",
      "section": "B",
      "the_json": {"name":"Jhon"}

Field name as key and its value. On Notepad, make a file and save it with the name import_example.json.


Now just import the JSON file import_example.json and your data will be imported in Django model.

Updated on: 26-Aug-2021

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started