DateTimeField received a naive datetime

感情迁移 提交于 2019-11-30 17:17:33

Further to falsetru's answer, if the datetime has already been created you can convert it to timezone aware:

from django.utils import timezone
my_datetime = timezone.make_aware(my_datetime, timezone.get_current_timezone())

Use django.utils.timezone.now instead of datetime.datetime.now.

from django.utils import timezone
current_time = timezone.now()
J0ANMM

You can also make the datetime time zone aware with localize from pytz, as explained here.

UTC:

import pytz
dt_aware = pytz.utc.localize(dt_naive)

Any other time zone:

import pytz
tz = 'Europe/Berlin' #or whaterver timezone you want
dt_aware = pytz.timezone(tz).localize(dt_naive)

And here the list of timezones.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!