我有以下功能:
import datetime
today = datetime.datetime.today
start_time = today().replace(hour=0, minute=0, second=0)
end_time = today().replace(hour=23, minute=59, second=59)
保存模型时:
models.Action(
created=today()
)
我收到以下警告:
/Users/user/Documents/workspace/app/env/lib/python2.7/site-packages/django/db/models/fields/__init__.py:1474: RuntimeWarning: DateTimeField action.created received a naive datetime (2015-04-20 00:00:00.176591) while time zone support is active.
RuntimeWarning)
/Users/user/Documents/workspace/app/env/lib/python2.7/site-packages/django/db/models/fields/__init__.py:1474: RuntimeWarning: DateTimeField action.created received a naive datetime (2015-04-20 23:59:59.176615) while time zone support is active.
RuntimeWarning)
你能解释一下为什么我会收到这个警告,以及如何使用我的例子来解决它?
我正在使用Django 1.8
答案 0 :(得分:2)
today = datetime.datetime.today
为您提供了一个天真的日期时间对象,它不知道时区。有了时区支持,Django希望获得时区感知日期时间对象。
使用django.utils.timezone
中的utils获取时区感知对象:
https://docs.djangoproject.com/en/1.8/ref/utils/#module-django.utils.timezone