我要保存以下日期:
timestamp = datetime.datetime.strptime(timestamp_raw, '%Y-%m-%dT%H:%M:%SZ')
当我保存时,我收到以下警告:
/Library/Python/2.7/site-packages/django/db/models/fields/__init__.py:808:
RuntimeWarning: DateTimeField received a naive datetime
(2014-12-11 21:42:58) while time zone support is active. RuntimeWarning)
如何将我的日期时间对象转换为支持tz的日期时间对象?
答案 0 :(得分:2)
Django为此提供utility function:
from django.utils.timezone import make_aware
timestamp = make_aware(timestamp)
默认情况下,这将使用current time zone,默认情况下为TIME_ZONE
。如果那不是您想要的,您可以将所需的时区作为参数传递给make_aware
。