Loaddata无法正确处理时间戳和时区 [英] Loaddata not dealing with timestamps and timezones properly

查看:117
本文介绍了Loaddata无法正确处理时间戳和时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用django 1.4.1并启用mysql和时区。我将数据转储到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?

推荐答案

文档 ...


序列化一个已知的日期时间时,将包括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"


...因此而不是...

...so instead of...

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'

...将起作用。

这篇关于Loaddata无法正确处理时间戳和时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆