heroku django 时间戳未转换为 UTC
heroku django timestamps not converting to UTC
我有一个 Django 应用程序(运行 在带有 Postgres 的 Heroku 上),它有一个包含 DateTimeField() 的简单表单:
class Response(models.Model):
'''
Model serves to record a users response to a given question on a given
questionnaire.
'''
user = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.PROTECT,
default=None,
db_index=True,
)
date = models.DateTimeField(
blank=False,
auto_now_add=True,
db_index=True,
help_text=(
"Date on which question was answered."
)
)
据我所知,我的 date
字段应该会自动填充当前日期时间
For DateTimeField: default=timezone.now - from django.utils.timezone.now()
-- ref
另外,应该是UTC时间戳
If USE_TZ is True, this will be an aware datetime representing the current time in UTC.
-- ref
我的settings.py有
TIME_ZONE = 'UTC'
USE_TZ = True
我有一些中间件可以将当前时区设置为 CST:
from django.utils import timezone
import pytz
class TimezoneMiddleware(object):
def process_request(self, request):
if request.user.is_authenticated():
timezone.activate(pytz.timezone('America/Chicago'))
else:
timezone.deactivate()
我在表单提交 [print(now(), localtime(now()), get_current_timezone())
] 之前使用了一些调试打印语句来验证 now()
实际上是 UTC。
当我提交表单时,我看到:
2018-03-06T16:56:23.569311+00:00 app[web.1]: 2018-03-06 16:56:23.569131+00:00 2018-03-06 10:56:23.569145-06:00 America/Chicago
但是数据库存储 2018-03-06 10:56:23
如何确保我提交的时间戳正确转换为 UTC?
however the database stores 2018-03-06 10:56:23
我会更仔细地研究一下;根据您获取该值的方式,它可能是也可能不是实际存储在数据库中的内容。数据库驱动程序(当然还有 Django 本身)可以从数据库中更改时区。如果它来自数据库驱动程序,请检查该值的时区,您可能会发现根本没有问题。
我有一个 Django 应用程序(运行 在带有 Postgres 的 Heroku 上),它有一个包含 DateTimeField() 的简单表单:
class Response(models.Model):
'''
Model serves to record a users response to a given question on a given
questionnaire.
'''
user = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.PROTECT,
default=None,
db_index=True,
)
date = models.DateTimeField(
blank=False,
auto_now_add=True,
db_index=True,
help_text=(
"Date on which question was answered."
)
)
据我所知,我的 date
字段应该会自动填充当前日期时间
For DateTimeField: default=timezone.now - from django.utils.timezone.now()
-- ref
另外,应该是UTC时间戳
If USE_TZ is True, this will be an aware datetime representing the current time in UTC.
-- ref
我的settings.py有
TIME_ZONE = 'UTC'
USE_TZ = True
我有一些中间件可以将当前时区设置为 CST:
from django.utils import timezone
import pytz
class TimezoneMiddleware(object):
def process_request(self, request):
if request.user.is_authenticated():
timezone.activate(pytz.timezone('America/Chicago'))
else:
timezone.deactivate()
我在表单提交 [print(now(), localtime(now()), get_current_timezone())
] 之前使用了一些调试打印语句来验证 now()
实际上是 UTC。
当我提交表单时,我看到:
2018-03-06T16:56:23.569311+00:00 app[web.1]: 2018-03-06 16:56:23.569131+00:00 2018-03-06 10:56:23.569145-06:00 America/Chicago
但是数据库存储 2018-03-06 10:56:23
如何确保我提交的时间戳正确转换为 UTC?
however the database stores
2018-03-06 10:56:23
我会更仔细地研究一下;根据您获取该值的方式,它可能是也可能不是实际存储在数据库中的内容。数据库驱动程序(当然还有 Django 本身)可以从数据库中更改时区。如果它来自数据库驱动程序,请检查该值的时区,您可能会发现根本没有问题。