Data import from URL, local CSV and JSON files into DB- Django app

Published Oct 27, 2017
Data import  from URL, local CSV and JSON files into DB- Django app

Steps to import data into your Django app
So you have a Django app and want to load data from a URL, a CSV file or a JSON file, I got you ๐Ÿ™‚
First, create your models, make migrations, migrate and all that setup you have to do, but Iโ€™m sure you got all that covered ๐Ÿ˜‰
See it on Github here:

Import from URL:
This code gets data from a URL, be sure to have requests installed or any other HTTP library of choice.

Import json data from URL to Datababse
import requests
import json
from import_data.models import Movie #Import your model here
from import BaseCommand
from datetime import datetime
IMPORT_URL = '' # URL to import from
class Command(BaseCommand):
  def import_movie(self, data):
  title = data.get('title', None)
  url = data.get('url', None);
  release_year =
  try: #try and catch for saving the objects
    movie, created = Movie.objects.get_or_create(
    if created:
     display_format = "\nMovie, {}, has been saved.
  exceptExceptionas ex:
    msg = "\n\nSomething went wrong saving this movie: {}\n{}".format(title, str(ex))
  def handle(self, *args, **options):
  Makes a GET request to the API.
  headers = {'Content-Type': 'application/json'}
  response = requests.get(
  data = response.json()
  for data_object in data:

to run this command, do: <the_file_name> (without the .py extension
so if file was, i'd run the command as: importer

NB: mind the code indentation on your IDE
continue reading:

Again, here is the github repo with all the import scripts and sample files:
Happy coding ๐Ÿ™‚

Discover and read more posts from Happiness Nwosu C.
get started