I get the following error when i try to use the "deleted" part of my json file to change one field (ReportedStatusField) for each "Facility" in my Database that matches with the id's inside "deleted":
File "C:\users\management\commands\deactivate_facilities.py", line 42, in handle
for key, data in data_object.items():
AttributeError: 'list' object has no attribute 'items'
It's basically a list of UUID's of Facilities that closed so i change the status this way with a json file that gets imported with a url.
import requests
import json
from users.models import Facility
from django.core.management.base import BaseCommand
IMPORT_URL = 'https://domain/file.json'
class Command(BaseCommand):
def import_facility_from_file(self, data):
key = data.get('key', None)
if Facility.objects.filter(key=UUID):
msg = "\n\nFacility closed: {}\n{}".format(key, str())
print(msg)
facility, facility_closed = Facility.objects.update_or_create(key=UUID,
defaults={
'ReportedStatusField': 'closed'
}
)
def handle(self, *args, **options):
headers = {'Content-Type': 'application/json'}
response = requests.get(
url=IMPORT_URL,
headers=headers,
)
response.raise_for_status()
data = response.json()
for key, data_object in data.items():
if key in ["deleted"]:
for key, data in data_object.items():
self.import_facility_from_file(data)
My JSON
{
"added":
{"125hk24h5kjh43k5":
{
"UUID":"125hk24h5kjh43k5",
"Name":"Test Facility 1",
"AddressInfo":
{"PrimaryAddress":"1234 Drive RD"},
"ImporterLastModifiedTimestamp":1643721420}},
// This is the "list" of deleted Facilities
"deleted":["235hk24h5kjh43k5,235hk345789h43k5"],
"modified":{"995hk24h5kjh43k5":
{
"UUID":"995hk24h5kjh43k5",
"Name":"Test Facility 2",
"AddressInfo":
{"PrimaryAddress":"2345 Test RD"},
"ImporterLastModifiedTimestamp":1643721420}
}
}
CodePudding user response:
Whenever we use 'items' method on the dictionary, it will return the tuple type element in the list. and we cannot run 'item' method on the list
for key, data_object in data.items():
if key in ["deleted"]:
for data in data_object:
self.import_facility_from_file(data)