Django DRF JSONRenderer';返回列表';对象没有属性';获取';
我正试图遵循Django DRF JSONRenderer';返回列表';对象没有属性';获取';,django,django-rest-framework,Django,Django Rest Framework,我正试图遵循Django Rest框架和VueJS的教程,使它们能够协同工作。在使用JSONRendererrender时,函数现在给了我一个问题: AttributeError: 'ReturnList' object has no attribute 'get' 我的列表视图是: from rest_framework import status from rest_framework.generics import ListAPIView, RetrieveAPIView from re
Django Rest框架
和VueJS
的教程,使它们能够协同工作。在使用JSONRenderer
render
时,函数现在给了我一个问题:
AttributeError: 'ReturnList' object has no attribute 'get'
我的列表视图是:
from rest_framework import status
from rest_framework.generics import ListAPIView, RetrieveAPIView
from rest_framework.permissions import AllowAny
from rest_framework.response import Response
from .models import Cat
from .renderers import CatJSONRenderer
from .serializers import CatSerializer, CatListSerializer
class CatListApiView(ListAPIView):
model = Cat
queryset = Cat.objects.all()
permissions_classes = (AllowAny, )
renderer_classes = (CatJSONRenderer, )
serializer_class = CatListSerializer
我的renderers.py是:
class CatJSONRenderer(JSONRenderer):
charset = 'utf-8'
def render(self, data, media_type=None, renderer_context=None):
print(data)
errors = data.get('errors', None)
if errors is not None:
return super(CatJSONRenderer, self).render(data)
return json.dumps({'cats': data})
回溯是:
Traceback:
File "/home/ytsejam/.virtualenvs/londonyogavidya/lib/python3.7/site-packages/django/core/handlers/exception.py" in inner
34. response = get_response(request)
File "/home/ytsejam/.virtualenvs/londonyogavidya/lib/python3.7/site-packages/django/core/handlers/base.py" in _get_response
156. response = self.process_exception_by_middleware(e, request)
File "/home/ytsejam/.virtualenvs/londonyogavidya/lib/python3.7/site-packages/django/core/handlers/base.py" in _get_response
154. response = response.render()
File "/home/ytsejam/.virtualenvs/londonyogavidya/lib/python3.7/site-packages/django/template/response.py" in render
106. self.content = self.rendered_content
File "/home/ytsejam/.virtualenvs/londonyogavidya/lib/python3.7/site-packages/rest_framework/response.py" in rendered_content
72. ret = renderer.render(self.data, accepted_media_type, context)
File "/home/ytsejam/public_html/londoner/yogavidya/apps/cat/renderers.py" in render
7. errors = data.get('errors', None)
Exception Type: AttributeError at /api/cats/
Exception Value: 'ReturnList' object has no attribute 'get'
我怎样才能解决这个问题?谢谢我可以通过安装
django rest framework json api来解决这个问题。
如果有人遇到问题,这就是将dict
转换为json我可以通过安装django rest framework json api来解决这个问题。
如果有人遇到问题,这将dict
转换为JSON
这可能不是解决此问题的正确方法,但它是一个或多或少类似于解决方法的建议。我发现,在不分页资源的情况下,它返回类型为
。呈现程序
类的呈现()方法
可以处理的实际预期输出是
。我所做的是使用资源实现分页,因为分页返回
。下面的代码片段是我的代码示例,供搜索者查看
class CategoryList(MyListCreateAPIView):
#without the pagination implementation
queryset = Category.objects.all()
serializer_class = CategorySerializer
... # for brevity
renderer_classes = (CategoryJSONRenderer,)
初始资源url为http://localhost:8000/categories/
class CategoryList(MyListCreateAPIView):
# with pagination implementation
... # for brevity
pagination_class = StandardResultsSetPagination
资源url现在看起来像http://localhost:8000/categories/?limit=5
。
对资源进行分页完成了全部工作
整个解决方案都是由made by提出的。这可能不是解决此问题的正确方法,但它是一个或多或少类似于解决方案的建议。我发现,在不分页资源的情况下,它返回类型为
。呈现程序
类的呈现()方法
可以处理的实际预期输出是
。我所做的是使用资源实现分页,因为分页返回
。下面的代码片段是我的代码示例,供搜索者查看
class CategoryList(MyListCreateAPIView):
#without the pagination implementation
queryset = Category.objects.all()
serializer_class = CategorySerializer
... # for brevity
renderer_classes = (CategoryJSONRenderer,)
初始资源url为http://localhost:8000/categories/
class CategoryList(MyListCreateAPIView):
# with pagination implementation
... # for brevity
pagination_class = StandardResultsSetPagination
资源url现在看起来像http://localhost:8000/categories/?limit=5
。
对资源进行分页完成了全部工作
整个解决方案由made by导入。请显示视图。正如错误所说,看起来data
是ReturnList的一个实例,而不是dict。json.dumps()
将为您提供一个字符串。字符串没有“get”属性。您是否尝试过返回{'cats':data}
?我尝试过同样的错误。@错误发生在该行之前。错误发生在错误行中。请显示视图。正如错误所说,看起来data
是ReturnList的一个实例,而不是dict。json.dumps()
将为您提供一个字符串。字符串没有“get”属性。您是否尝试过返回{'cats':data}
?我尝试过同样的错误。@Rachel错误发生在该行之前。错误发生在错误行中。