如何解决“ dict”对象没有属性“ _meta” [英] how to solve the 'dict' object has no attribute '_meta'
问题描述
我正尝试从Web API检索一些数据,因为我想在浏览器中显示它们,并且我想通过将这些数据分配给变量来对其进行操作 ,所以我可以操纵数据,但出现错误:
I am trying to retrieve some data from a web API because I want to display it in the browser and I want to manipulate it by assigning this data to a variable, so I can manipulate the data but I am getting the error:
'dict'对象没有属性'_meta'。
观看次数
from django.core import serializers
from rest_framework.views import APIView
from rest_framework.response import Response
import json, urllib.request
from django.views.generic import View
from django.http import JsonResponse
def get_data(request, *args, **kwargs):
with urllib.request.urlopen("http://10.61.202.98:8081/T/ansdb/api/rows/dev/tickets?id=1003611",timeout=10) as url:
response_data = json.loads(url.read().decode())
response_data_serialized = serializers.serialize('json',response_data)
return JsonResponse(response_data_serialized, safe=False)
网址
urlpatterns = [
url(r'^$', views.index, name='index'), # home
url(r'^statistics/$', views.statistics, name='statistics'),
url(r'^statistics/data$', get_data, name='get_data'),]
我要检索的数据具有以下格式:
The data that I want to retrieve has the following format:
[{
id: 100361324,
Aging_Deferred_Transferred: "",
Aging_Open_Issue: "",
Aging_Un_investigated_Issue: "",
CreatedBy: "userx@.....com",
DeltaQD: null,
DeltaQDBadAttempts: null,
Escalation_Category: "",
Golden_Cluster: "",
Incident_DateTime: "2017-02-01
Week: "8",
Weekend_Flag: "Yes"
}]
我读到的某些链接是 Django JSON :: dict对象没有属性 _meta 和 Django1.9: function对象没有属性 _meta ,但是在那些链接中,解决方案不适合我的问题。
Some links I read were Django JSON:: 'dict' object has no attribute '_meta' and Django1.9: 'function' object has no attribute '_meta' but in those links the solutions do not fit my problem.
欢迎提供任何有关如何解决此问题的帮助。
Any help on how to solve this is welcome.
谢谢。
推荐答案
好像我没有必须序列化数据,并只需使用以下命令修改代码:
Seems like I did not have to serialize the data and just modify the code with the following:
def get_data(request, *args, **kwargs):
with urllib.request.urlopen("http://10.61.202.98:8081/T/ansdb/api/rows/dev/tickets?id=1003611",timeout=10) as url:
response_data = json.loads(url.read().decode())
print(response_data)
return JsonResponse(response_data, safe=False)
这可以在应用程序的当前窗口中可视化JSON,但仍无法在控制台中看到此JSON对象。
This makes possible to visualize the JSON in the current window of my app but I am still unable to see this JSON object in the console.
这篇关于如何解决“ dict”对象没有属性“ _meta”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!