【已解决】django 后台view.py中将数据转为json报错:TypeError: Object of type datetime is not JSON serializable。

2023-10-29

【已解决】django 后台view.py中将数据转为json报错:TypeError: Object of type datetime is not JSON serializable。之后又报错TypeError: In order to allow non-dict objects to be serialized set the safe parameter to False.


问题描述

 在django后台的view.py中,需要实现查询数据库中的数据,并转为json数据传递回前台页面。但是在使用json.dumps方法将数据转为json时,报错。我的代码是这样写的:

data = TIndex.objects.all().values()
data = list(data)
data = json.dumps(data, cls=DateEncoder)
return JsonResponse(data)

 报错的信息是这样的:

.........................................

data = json.dumps(data)
  File "D:\python\lib\json\__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
  File "D:\python\lib\json\encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  Fi

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

【已解决】django 后台view.py中将数据转为json报错:TypeError: Object of type datetime is not JSON serializable。 的相关文章

随机推荐