gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 22:28:24 36 4
gpt4 key购买 nike

我刚接触Django和DRF。在学习的时候,我发现了错误。我不确定错误是从哪里产生的。

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)

最佳答案

我认为您错过了访问序列化程序的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)
^^^^

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

关于python - DRF:“ListSerializer”类型的对象不是JSON可序列化的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55113657/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com