how to solve the 'dict' object has no attribute '_meta'

落花浮王杯 提交于 2019-12-25 18:26:53

问题


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' object has no attribute '_meta'.

Views

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)

urls

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"
}]

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.

Thank you in advance.


回答1:


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)

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.



来源:https://stackoverflow.com/questions/44295448/how-to-solve-the-dict-object-has-no-attribute-meta

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!