Te damos la bienvenida a nuestra comunidad, en este sitio vas a encontrar la respuesta que necesitas.
Ejemplo: django import json
"""
Import json data from URL to Datababse
"""import requests
import json
from import_data.models import Movie #Import your model herefrom django.core.management.base import BaseCommand
from datetime import datetime
IMPORT_URL ='https://jsonplaceholder.typicode.com/photos'# URL to import fromclassCommand(BaseCommand):defimport_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 ="nnSomething went wrong saving this movie: n".format(title,str(ex))print(msg)defhandle(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)
Recuerda que tienes la capacidad de aclarar si descubriste tu incógnita a tiempo.
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)