Importing data into models in Django

PythonServer Side ProgrammingProgramming

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 views.py, urls.py or any html file. We only have to work with settings.py, admin.py, models.py and admin urlpoint.

Example

Install the django-import-export package −

pip install django-import-export

In settings.py, 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):
   name=models.CharField(max_length=100)
   standard=models.CharField(max_length=100)
   section=models.CharField(max_length=100)

We created the model for testing and trying.

In admin.py

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

admin.site.register(StudentData,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",
      "standard":"10",
      "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.

Output

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

raja
Published on 26-Aug-2021 12:09:49
Advertisements