我如何像这样在unix epoch datetime格式中保存
2019-11-12T10:26:39.613Z
准确地放入模型.DateTimeField
我尝试过此操作,但是如您所见,使用datetime.datetime.strptime进行分配似乎是正确的,但是在保存之后,datetime值发生了变化(然后不准确)
>>> b.updated_at = datetime.datetime.strptime("2019-11-12T10:26:39.613Z", '%Y-%m-%dT%H:%M:%S.%fZ')
>>> b.updated_at
datetime.datetime(2019, 11, 12, 10, 26, 39, 613000)
>>> b.save()
>>> b.updated_at
datetime.datetime(2019, 11, 12, 3, 1, 35, 82434, tzinfo=<UTC>)
在model.py中
updated_at = models.DateTimeField(auto_now=True)
我正在使用Django 1.11和Postgres 9.4
您的问题不在于您的日期格式。您的问题是您已声明字段为auto_now=True
。这导致Django在保存时始终将其设置为当前时间,从而覆盖了内存中的值。从the docs: