在网站开发时一些model中是存在DateTimeField类型的,在用的过程中读取显示也没发现任何问题。
但是在数据统计时却发现了问题
事情是这样的:
我在9月1日记录了一条数据,但是在统计9月数据记录的情况时却发现没有任何记录。很奇怪,于是查看库里面,原来时间的格式存储的是utc格式的。也就是往前8小时。这样数据就算在了8月份当中。
于是在manage.py shell中试着存储时间类型的数据时,有个warning
DateTimeField received a naive datetime ... while time zone support is active.在Python27\Lib\site-packages\django\db\models\fields\__init__.py中找到报警的地方,看到如下代码:
if settings.USE_TZ:
# For backwards compatibility, interpret naive datetimes in
# local time. This won't work during DST change, but we can't
# do much about it, so we let the exceptions percolate up the
# call stack.
warnings.warn(u"DateTimeField received a naive datetime (%s)"
u" while time zone support is active." % value,
RuntimeWarning)
default_timezone = timezone.get_default_timezone()
value = timezone.make_aware(value, default_timezone)
return value
想必是settings.USE_TZ设置问题了,于是在settings中设置为False,后来存储的时间格式就是我们自己本地的了。
在官网中有1.4中支持time-zone的描述及原因:
https://docs.djangoproject.com/en/1.4/releases/1.4/