Loaddata 未正确处理时间戳和时区

2022-01-14 00:00:00 python django yaml

问题描述

我正在使用启用了 mysql 和时区的 django 1.4.1.我将数据转储到 yaml,修改了一些字段以创建一些测试数据,并尝试将其重新加载.然而,即使指定了 tz,Django 仍然抱怨天真的日期时间

I'm using django 1.4.1 with mysql and timezones enabled. I did a dump data to yaml, modified some fields to create some test data, and am trying to load it back in. however, Django keeps complaining about naive datetimes even though a tz is specified

具体来说,我的 loaddata 有:

specifically, my loaddata has:

fields: {created_date: !!timestamp '2012-09-15 22:17:44+00:00', ...

但是 loaddata 给出了错误:

but loaddata gives the error:

RuntimeWarning: DateTimeField received a naive datetime (2012-09-15 22:17:44) while time zone support is active.

这对我来说没有多大意义,因为它是:

This doesn't make much sense to me, seeing as its:

  1. UTC 时间戳
  2. 与 Django 使用 dumpdata 导出的完全相同的格式

有什么方法可以告诉 django 这是一个 UTC 日期吗?

is there some way i can tell django this is a UTC date?


解决方案

来自 docs...

在序列化感知日期时间时,包括 UTC 偏移量,例如这个:

When serializing an aware datetime, the UTC offset is included, like this:

"2011-09-01T13:20:30+03:00"

对于一个天真的日期时间,显然不是:

For a naive datetime, it obviously isn't:

"2011-09-01T13:20:30"

...所以不是...

created_date: !!timestamp '2012-09-15 22:17:44+00:00'

...任何一个...

created_date: '2012-09-15T22:17:44+00:00'

...或...

created_date: '2012-09-15T22:17:44Z'

...会起作用的.

相关文章