DRF:“ ListSerializer”类型的对象不可JSON序列化

时间:2019-03-12 03:23:23

标签: python django django-rest-framework django-serializer

我是Django和DRF的新手。在学习时遇到错误Object of type 'ListSerializer' is not JSON serializable错误。我不确定错误从何处引发

Traceback:

File "/home/marvel/venv/django_1_111/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
  41.             response = get_response(request)

File "/home/marvel/venv/django_1_111/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  217.                 response = self.process_exception_by_middleware(e, request)

File "/home/marvel/venv/django_1_111/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  215.                 response = response.render()

File "/home/marvel/venv/django_1_111/lib/python3.6/site-packages/django/template/response.py" in render
  107.             self.content = self.rendered_content

File "/home/marvel/venv/django_1_111/lib/python3.6/site-packages/rest_framework/response.py" in rendered_content
  72.         ret = renderer.render(self.data, accepted_media_type, context)

File "/home/marvel/venv/django_1_111/lib/python3.6/site-packages/rest_framework/renderers.py" in render
  718.         context = self.get_context(data, accepted_media_type, renderer_context)

File "/home/marvel/venv/django_1_111/lib/python3.6/site-packages/rest_framework/renderers.py" in get_context
  675.             'content': self.get_content(renderer, data, accepted_media_type, renderer_context),

File "/home/marvel/venv/django_1_111/lib/python3.6/site-packages/rest_framework/renderers.py" in get_content
  416.         content = renderer.render(data, accepted_media_type, renderer_context)

File "/home/marvel/venv/django_1_111/lib/python3.6/site-packages/rest_framework/renderers.py" in render
  105.             allow_nan=not self.strict, separators=separators

File "/home/marvel/venv/django_1_111/lib/python3.6/site-packages/rest_framework/utils/json.py" in dumps
  28.     return json.dumps(*args, **kwargs)

File "/usr/lib/python3.6/json/__init__.py" in dumps
  238.         **kw).encode(obj)

File "/usr/lib/python3.6/json/encoder.py" in encode
  201.             chunks = list(chunks)

File "/usr/lib/python3.6/json/encoder.py" in _iterencode
  437.             o = _default(o)

File "/home/marvel/venv/django_1_111/lib/python3.6/site-packages/rest_framework/utils/encoders.py" in default
  68.         return super(JSONEncoder, self).default(obj)

File "/usr/lib/python3.6/json/encoder.py" in default
  180.                         o.__class__.__name__)

Exception Type: TypeError at /sample/view/
Exception Value: Object of type 'ListSerializer' is not JSON serializable

这是我的代码示例

#serializer.py
class SampleSerializer(serializers.ModelSerializer):
    class Meta:
        fields = '__all__'
        model = SampleModel

#views.py
@api_view(http_method_names=['GET'])
def my_view(request):
    qs = SampleModel.objects.all()
    serializer = SampleSerializer(qs, many=True)
    return Response(serializer)

1 个答案:

答案 0 :(得分:4)

我认为您已经错过了访问序列化程序的 data 属性的权限。也就是说,它应该是 Response(serializer.data) 而不是Response(serializer)

代码

@api_view(http_method_names=['GET'])
def my_view(request):
    qs = SampleModel.objects.all()
    serializer = SampleSerializer(qs, many=True)
    return Response(serializer.data)
                               ^^^^

要获取序列化数据,我们必须访问序列化程序的 data 属性。