Importing data into models in Django

In this article, we will learn how to import data from JSON format into Django models. Django supports importing data from various formats including JSON, CSV, XLSX, and YAML using the django-import-export package.

We'll create a Django project with a simple model and configure it to accept imported data through the admin interface.

Setup Requirements

First, install the required package ?

pip install django-import-export

Add the package to your Django project's INSTALLED_APPS in settings.py ?

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'import_export',  # Add this line
    'myapp',  # Your app name
]

Creating the Model

Create a simple model in models.py ?

from django.db import models

class StudentData(models.Model):
    name = models.CharField(max_length=100)
    standard = models.CharField(max_length=100)
    section = models.CharField(max_length=100)
    
    def __str__(self):
        return f"{self.name} - {self.standard}{self.section}"

Configuring Admin Interface

Configure the admin interface to support import/export functionality 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
    list_display = ['name', 'standard', 'section']

admin.site.register(StudentData, StudentAdmin)

JSON File Format

Create a JSON file with the following structure and save it as students_data.json ?

[
    {
        "id": 1,
        "name": "John Doe",
        "standard": "10",
        "section": "A"
    },
    {
        "id": 2,
        "name": "Jane Smith",
        "standard": "10",
        "section": "B"
    },
    {
        "id": 3,
        "name": "Mike Johnson",
        "standard": "11",
        "section": "A"
    }
]

Import Process

Follow these steps to import your data ?

  1. Run migrations: python manage.py makemigrations and python manage.py migrate
  2. Create a superuser: python manage.py createsuperuser
  3. Start the development server: python manage.py runserver
  4. Navigate to /admin and log in
  5. Go to the StudentData model page
  6. Click the "Import" button
  7. Upload your JSON file and follow the import wizard

Key Features

Feature Description
Format Support JSON, CSV, XLSX, YAML, ODS
Validation Validates data before import
Preview Shows preview before final import
Error Handling Reports import errors with details

Conclusion

The django-import-export package provides a powerful and user-friendly way to import data into Django models. It supports multiple file formats and includes validation features to ensure data integrity during the import process.

Updated on: 2026-03-26T00:41:59+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements