Django 调试时提示: In order to allow non-dict objects to be serialized

Posted zhao159461

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Django 调试时提示: In order to allow non-dict objects to be serialized相关的知识,希望对你有一定的参考价值。

isinstance()函数:

该函数判断一个对象是否是已知的类型,类似于type()

isinstance()会认为子类是一种父类类型,考虑了继承关系,而type()不会认为子类是一种父类类型,不考虑继承关系。

isinstance(object,classinfo)object是实例对象,classinfo是直接或间接类名,基本类型或者由他们组成的元组。

return JsonResponse(result)变更为return JsonResponse(result,safe=False)
原因看源码:

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)

if safe and not isinstance(data, dict):如果safe=True 传入的数据类型不是dict,那么条件就成立,抛出异常

以上是关于Django 调试时提示: In order to allow non-dict objects to be serialized的主要内容,如果未能解决你的问题,请参考以下文章

Django 报错 Refused to display 'http://127.0.0.1:8000/ in a frame because it set 'X-Frame-Opti

Django 报错 Refused to display 'http://127.0.0.1:8000/ in a frame because it set 'X-Frame-Opti

如何在 Django 管理员中配置重新排序 order_with_respect_to 项目?

canvas order in layer 怎么动态获取

vmware virtual machine must be running in order to be migrated

How to change Google chrome windows order in the Windows task panel