首页 > Web开发 > 详细

TypeError: Object of type datetime is not JSON serializable问题解决

时间:2020-06-02 13:17:38      阅读:930      评论:0      收藏:0      [点我收藏+]

前提回顾

在进行django开发view视图时,如果数据库字段是 datetime类型,在JSON序列化返回时,会出现异常

异常现象

TypeError: Object of type datetime is not JSON serializable

异常原因

create_time: datetime.datetime(2020, 6, 1, 17, 27, tzinfo=<UTC>)

字段为datetime,无法序列化对象

问题解决

# 对时间进行格式化
# create_time 数据库表字段名
for t in Students.objects.all():
create_time: t.create_time.strftime(%Y-%m-%d %H:%M:%S),

 

TypeError: Object of type datetime is not JSON serializable问题解决

原文:https://www.cnblogs.com/fengzi-2016/p/13030379.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!