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

时间:2020-06-02 13:17:38   收藏:0   阅读:930

前提回顾

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

 

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

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!