2

I create a web application with Django and I need basic data. To do this, I have a JSON that contains a lot of data that I would like to import into my Django database.

So I'm looking for a way to import this data without modifying the JSON file.

Extract from JSON:

[
  {
    "key": "sword",
    "name": "Sword",
    "tier": 1
  },
  {
    "key": "bow",
    "name": "Bow",
    "tier": 1
  },
...
]

My file contains a lot of data and it would take a long time to change it. Is there a script / program to import all this data corresponding to the following model:

from django.db import models

class Item(models.Model):
    name = models.CharField(max_length=120)
    tier = models.PositiveIntegerField()

I saw the example on the django site, but it would be necessary to add to each item in my JSON a property model which would be very long

Example from Django website:

[
  {
    "model": "myapp.person",
    "pk": 1,
    "fields": {
      "first_name": "John",
      "last_name": "Lennon"
    }
  },
  {
    "model": "myapp.person",
    "pk": 2,
    "fields": {
      "first_name": "Paul",
      "last_name": "McCartney"
    }
  }
]

2 Answers 2

5

You could create a custom management command to import your custom JSON file. Docs.

import json
from django.core.management.base import BaseCommand
from myapp.models import Item

class Command(BaseCommand):

    def add_arguments(self, parser):
        parser.add_argument('json_file', type=str)

    def handle(self, *args, **options):
        with open(options['json_file']) as f:
            data_list = json.load(f)

        for data in data_list:
            data['pk'] = data.pop('key')
            Item.objects.get_or_create(pk=data['pk'], defaults=data)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for this, this is very useful. Is there a particular place where the json file should go? Or would you simply specify the directory in the place where it says ['json_file']
Sure, no problem. Yeah you can just specify the absolute path or the path relative to the manage.py. So assuming your management command file is called import_custom_stuff.py you can run this from you project root directory: python manage.py import_custom_stuff path/to/file.json.
0

You can create a custom migration file and parse/save data to db by yourself. Here is a sample migration file:

# your_custom_migration.py
import json
from __future__ import unicode_literals

from django.db import migrations
from django.core.management import call_command

def load_file(apps, schema_editor):

    YourModel = apps.get_model('your_app', 'YourModel')

    with open('data.json') as json_file:
        data = json.load(json_file)
        for p in data:
            # create YourModel object and save it to database
            your_model = YourModel.objects.create(key=p['key'], name=p['name'], tier=['tier']) 


class Migration(migrations.Migration):

    dependencies = [
        ('manager', 'your_previous_migration_that_you_depend'),
    ]

    operations = [
        migrations.RunPython(load_file, None)
    ]

Then you need to run: python manage.py migrate.

NOTE: Just make sure you depen on your latest migration file in:

dependencies = [
        ('manager', 'your_previous_migration_that_you_depend'),
    ]

1 Comment

I didn't understand how to do this import. Which command should I use? Moreover I get an error with the future import

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.