[英]"AttributeError: 'list' object has no attribute 'items'" While importing Json
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":当我尝试使用我的 json 文件的“已删除”部分为我的数据库中与“已删除”内的 ID 匹配的每个“设施”更改一个字段 (ReportedStatusField) 时,出现以下错误:
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.它基本上是关闭设施的 UUID 列表,因此我使用 json 文件以这种方式更改状态,该文件通过 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我的 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}
}
}
Whenever we use 'items' method on the dictionary, it will return the tuple type element in the list.每当我们在字典上使用'items'方法时,它都会返回列表中的元组类型元素。 and we cannot run 'item' method on the list
我们不能在列表上运行“item”方法
for key, data_object in data.items():
if key in ["deleted"]:
for data in data_object:
self.import_facility_from_file(data)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.