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

时间:2020-06-02 13:25:40   收藏:0   阅读:239

前提回顾

在进行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),

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!