how to solve the 'dict' object has no attr

2019-08-30 00:40发布

问题:

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.