Input Json data in django models

I have a json file with a huge number of data in it, I have also created a django model to store that data and now I can't figure out how to put that json data into my models. Doing it manually will consume a lot of my time.
However, I can use models.JSONField to put that data but that's not what I want. I want to put each data in a model field.
Here's a sample of my JSON data:

[
    {
        "end_year": "",
        "intensity": 6,
        "sector": "Energy",
        "topic": "gas",
        "insight": "Annual Energy Outlook",
        "url": "http://example.com",
        "region": "Northern America",
        "start_year": "",
        "impact": "",
        "added": "January, 20 2017 03:51:25",
        "published": "January, 09 2017 00:00:00",
        "country": "United States of America",
        "relevance": 2,
        "pestle": "Industries",
        "source": "EIA",
        "title": "Test phase",
        "likelihood": 3
    }
]

And this is my model:

class myModel(models.Model):
    end_year = models.PositiveIntegerField(null=True, blank=True)
    intensity = models.PositiveSmallIntegerField(null=True, blank=True)
    sector = models.CharField(max_length = 255, null=True, blank=True)
    topic = models.CharField(max_length = 55, null=True, blank=True)
    insight = models.TextField()
    url = models.URLField(max_length = 300)
    region = models.CharField(max_length = 50, null=True, blank=True)
    start_year = models.PositiveIntegerField(null=True, blank=True)
    impact = models.CharField(max_length = 255, null=True, blank=True)
    added = models.DateTimeField(null=True, blank=True)
    published = models.DateTimeField(null=True, blank=True)
    country = models.CharField(max_length = 50, null=True, blank=True)
    relevance = models.PositiveIntegerField(null=True, blank=True)
    pestle = models.CharField(max_length = 100, null=True, blank=True)
    source = models.CharField(max_length = 200, null=True, blank=True)
    title = models.CharField(max_length = 300, null=True, blank=True)
    likelihood = models.PositiveIntegerField(null=True, blank=True)

You can do something like below, please adjust your field if required:

import json
from myapp.models import myModel
from django.utils.dateparse import parse_datetime

def populate_database(json_file_path):
    with open(json_file_path, 'r') as file:
        data = json.load(file)

    # Bulk create instances to minimize database queries
    instances_to_create = []
    for item in data:
        instance = myModel(
            end_year=item.get('end_year'),
            intensity=item.get('intensity'),
            sector=item.get('sector'),
            topic=item.get('topic'),
            insight=item.get('insight'),
            url=item.get('url'),
            region=item.get('region'),
            start_year=item.get('start_year'),
            impact=item.get('impact'),
            added=parse_datetime(item.get('added')),
            published=parse_datetime(item.get('published')),
            country=item.get('country'),
            relevance=item.get('relevance'),
            pestle=item.get('pestle'),
            source=item.get('source'),
            title=item.get('title'),
            likelihood=item.get('likelihood'),
        )
        instances_to_create.append(instance)

    myModel.objects.bulk_create(instances_to_create)

# call it where required
populate_database('path_to_your_json_file.json')
Back to Top