【已解决】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