我正在尝试遵循Django Rest Framework
和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'
我该如何解决这个问题?谢谢
答案 0 :(得分:0)
我可以通过安装from bokeh.io import show
from bokeh.layouts import widgetbox
from bokeh.models import ColumnDataSource, Slider, DataTable, TableColumn, CustomJS
source = ColumnDataSource(dict(x = list(range(6)), y = [x ** 2 for x in range(6)]))
columns = [TableColumn(field = "x", title = "x"), TableColumn(field = "y", title = "x**2")]
table = DataTable(source = source, columns = columns, width = 320)
slider = Slider(start = 1, end = 100, value = 6, step = 1, title = "i", width = 300)
callback_code = """ i = slider.value;
new_data = {"x": [1, 2, 3, 4, 5], "y": [1, 4, 9, 16, 25]}
table.source.data = new_data
table.width = 320 + i * 25; """
callback = CustomJS(args = dict(slider = slider, table = table), code = callback_code)
slider.js_on_change('value', callback)
show(widgetbox(slider, table))
来解决此问题。如果有人遇到问题,这会将django-rest-framework-json-api.
转换为dict
。
答案 1 :(得分:0)
这可能不是解决此问题的正确方法,但它或多或少是一种解决方法。我发现,在不分页资源的情况下,它会返回data
类型的
<class 'rest_framework.utils.serializer_helpers.ReturnList'>
。 render()
类的renderer
方法可以处理的实际期望输出为<class 'collections.OrderedDict'>
。我所做的是在分页返回<class 'collections.OrderedDict'>
时用资源实现分页。下面的代码段是我的代码示例,供求职者审查。
class CategoryList(MyListCreateAPIView):
#without the pagination implementation
queryset = Category.objects.all()
serializer_class = CategorySerializer
... # for brevity
renderer_classes = (CategoryJSONRenderer,)
初始资源网址为http://localhost:8000/categories/
class CategoryList(MyListCreateAPIView):
# with pagination implementation
... # for brevity
pagination_class = StandardResultsSetPagination
资源网址现在看起来像http://localhost:8000/categories/?limit=5
。
分页资源可以解决整个问题。
Github comment制作的@jsenecal证明了整个解决方法。