django1.4中时间类型入库注意

在网站开发时一些model中是存在DateTimeField类型的,在用的过程当中读取显示也没发现任何问题。python

可是在数据统计时却发现了问题shell

事情是这样的:django

我在9月1日记录了一条数据,可是在统计9月数据记录的状况时却发现没有任何记录。很奇怪,因而查看库里面,原来时间的格式存储的是utc格式的。也就是往前8小时。这样数据就算在了8月份当中。网站

因而在manage.py shell中试着存储时间类型的数据时,有个warning code

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/ get

相关文章
相关标签/搜索