【问题标题】:Datetime is not json serializable, using tastypie日期时间不是 json 可序列化的,使用美味派
【发布时间】:2015-08-02 05:04:20
【问题描述】:

这是我的 time_now() 函数

def time_now():
    return timezone.now()

我调用它来初始化我的 SkyGroupResource 的 created_at 字段 用以下方式:

def obj_create(self,bundle,**kwargs):

    return super(SkyGroupResource, self).obj_create(bundle, owner_id=bundle.request.user.skyuser, created_at = time_tools.time_now())

在其他资源中,我有以下字段:

group_id = fields.ForeignKey(SkyGroupResource,'group_id',null=False)

当我尝试在 POST 上使用我的第二个资源时,我收到以下错误

TypeError: datetime.datetime(2015, 5, 19, 19, 51, 33, 8253, tzinfo=) 不是 JSON 可序列化的

有什么想法吗?

【问题讨论】:

    标签: python json django tastypie


    【解决方案1】:

    您可以先将日期转换为字符串,例如使用str(datetime.now()).

    *编辑:你应该看看:How to overcome "datetime.datetime not JSON serializable" in python?。

    【讨论】:

      猜你喜欢
      • 2019-06-25
      • 1970-01-01
      • 2012-08-14
      • 1970-01-01
      • 2012-03-07
      • 1970-01-01
      • 2019-03-22
      • 1970-01-01
      • 2019-10-26
      相关资源
      最近更新 更多