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: https://github.com/NEbere/data-import/tree/master

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 django.core.management.base import BaseCommand
from datetime import datetime
 
IMPORT_URL = 'https://jsonplaceholder.typicode.com/photos' # URL to import from
 
class Command(BaseCommand):
  def import_movie(self, data):
  title = data.get('title', None)
  url = data.get('url', None);
  release_year = datetime.now()
  try: #try and catch for saving the objects
    movie, created = Movie.objects.get_or_create(
    title=title,
    url=url,
    release_year=release_year
    )
    if created:
      movie.save()
     display_format = "\nMovie, {}, has been saved.
     print(display_format.format(movie))
  exceptExceptionas ex:
    print(str(ex))
    msg = "\n\nSomething went wrong saving this movie: {}\n{}".format(title, str(ex))
    print(msg)
  def handle(self, *args, **options):
  """
  Makes a GET request to the API.
  """
  headers = {'Content-Type': 'application/json'}
  response = requests.get(
  url=IMPORT_URL,
  headers=headers,
  )
  response.raise_for_status()
  data = response.json()
  for data_object in data:
  self.import_movie(data_object)

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

manage.py importer

NB: mind the code indentation on your IDE
continue reading:
http://happinessnwosu.com/2017/10/17/import-data-into-database-django-app/

Again, here is the github repo with all the import scripts and sample files:
https://github.com/NEbere/data-import/tree/master
Happy coding šŸ™‚

Discover and read more posts from Happiness Nwosu C.
get started
Enjoy this post?

Leave a like and comment for Happiness

1
6