快活林资源网 Design By www.csstdc.com

代码

return JsonResponse({"name": "tom"})

报错:

TYPEERROR: In order to allow non-dict objects to be serialized

set the safe parmeter to False

解决:

return JsonResponse({"name": "tom"}, safe=False)

增加safe=false,使其接受列表

补充知识:python 里面 JsonResponse (book_list,safe=False)

代码为:

# 查询所有图书 、 增加图书
def get(self,request):

  queryset = BookInfo.objects.all()
  book_list = []

  for book in queryset:
    book_list.append({
      'id':book.id,
      'bread':book.bread

    })
  return JsonResponse (book_list,safe=False)
 

遇到问题:

JsonResponse (book_list,safe=False)

safe=False 这是什么鬼 ?

解决方案:

down 下源码后 :

def __init__(self, data, encoder=DjangoJSONEncoder, safe=True,
       json_dumps_params=None, **kwargs):
  if safe and not isinstance(data, dict):
    raise TypeError(
      'In order to allow non-dict objects to be serialized set the '
      'safe parameter to False.'
    )
 
 if json_dumps_params is None:
    json_dumps_params = {}
  kwargs.setdefault('content_type', 'application/json')
  data = json.dumps(data, cls=encoder, **json_dumps_params)
  super(JsonResponse, self).__init__(content=data, **kwargs)

最终答案:

'In order to allow non-dict objects to be serialized set the ' 'safe parameter to False.'

以上这篇解决Django响应JsonResponse返回json格式数据报错问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

快活林资源网 Design By www.csstdc.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
快活林资源网 Design By www.csstdc.com