I'm trying to import data from a json file URL into my Django Database on a weekly basis to keep my Database up to date. That Data is coming from an Importer which was written in GO and which provides me with that JSON File via a link.
I still don't have a way to automate this or to make sure how to check if an item got updated or not and then only update those items. But that is not my main issue right now. My main issue is that i'm receiving the following error every time i try to import the data with the manage.py command:
File "\data_import\management\commands\import_from_url.py", line 45, in handle
self.import_facility_from_file()
File "\data_import\management\commands\import_from_url.py", line 21, in import_facility_from_file
Name = data_object.get('Name', None)
AttributeError: 'str' object has no attribute 'get'
This is my Code:
import os
import json
from data_import.models import Facility
from django.core.management.base import BaseCommand
from datetime import datetime
from jsontest.settings import BASE_DIR, STATIC_URL
class Command(BaseCommand):
def import_facility_from_file(self):
print(BASE_DIR)
data_folder = os.path.join(BASE_DIR, 'import_data', 'resources')
for data_file in os.listdir(data_folder):
with open(os.path.join(data_folder, data_file), encoding='utf-8') as data_file:
data = json.loads(data_file.read())
for data_object in data:
Name = data_object.get('Name', None)
IssuedNumber = data_object.get('IssuedNumber', None)
release_year = datetime.now()
try:
Facility, created = Facility.objects.get_or_create(
Name=Name,
IssuedNumber=IssuedNumber,
release_year=release_year
)
if created:
Facility.save()
display_format = "\Facility, {}, has been saved."
print(display_format.format(Facility))
except Exception as ex:
print(str(ex))
msg = "\n\nSomething went wrong saving this Facility: {}\n{}".format(Name, str(ex))
print(msg)
def handle(self, *args, **options):
"""
Call the function to import data
"""
self.import_facility_from_file()
The JSON looks like this:
{"00016ed7be4d872aseddsf6f36bfsdfd647f3eb41cadedd2130bdfsdfsdf6fbbbf24c2f1a64d2cf34ac4e03aaa30309816f98a7389b2bb324a2":{"UUID":"00016ed7be4d872aseddsf6f36bfsdfd647f3eb41cadedd2130bdfsdfsdf6fbbbf24c2f1a64d2cf34ac4e03aaa30309816f98a7389b2bb324a2","Name":"SENIOR CARE","IssuedNumber":"123456","Licensee":"SENIOR CARE","Email":"[email protected]","AdministratorName":"Tester","TelephoneNumber":"(123) 456-789101112","AddressInfo":{"PrimaryAddress":"123 Test Road","SecondaryAddress":"","City":"Testcity","RegionOrState":"TESTSTATE","PostalCode":"12345","Geolocation":"11.1111,-11.1111"},"Capacity":1,"MostRecentLicenseTimestamp":1234567891,"ClosedTimestamp":0,"InspectionInfo":{"ComplaintRelatedVisits":0,"InspectionRelatedVisits":0,"NumberOfVisits":0,"LastVisitTimestamp":0},"Complaints":{"ComplaintsTypeA":0,"ComplaintsTypeB":0,"SubstantiatedAllegations":0,"TotalAllegations":0}},