Django 查询正在从数据库获取UTC时间戳,但本地时间存储在数据库中
出于某种原因,我的queryset返回UTC时间,但是在数据库中,它应该得到的时间是本地时间。有人知道为什么会这样吗?谢谢你的帮助 last_checkin_time方法是从数据库中获取用户最后一次签入时间戳的方法,现在我只是将它发布在我的toggle方法中的time_delta变量中,这样我就可以看到它得到了什么值。(一旦我计算出这个时区,时间增量将是实际的时间增量) 这是我的模特经理Django 查询正在从数据库获取UTC时间戳,但本地时间存储在数据库中,django,django-models,Django,Django Models,出于某种原因,我的queryset返回UTC时间,但是在数据库中,它应该得到的时间是本地时间。有人知道为什么会这样吗?谢谢你的帮助 last_checkin_time方法是从数据库中获取用户最后一次签入时间戳的方法,现在我只是将它发布在我的toggle方法中的time_delta变量中,这样我就可以看到它得到了什么值。(一旦我计算出这个时区,时间增量将是实际的时间增量) 这是我的模特经理 class UserActivityManager(models.Manager): def cu
class UserActivityManager(models.Manager):
def current(self, user):
current_obj = self.get_queryset().filter(user=user).order_by('-timestamp').first()
return current_obj
def last_checkin_time(self, user):
last_activity_time = self.get_queryset().order_by('-timestamp').filter(user=user, activity="checkin").first()
return last_activity_time
def toggle(self, user):
last_item = self.current(user)
activity = "checkin"
time_delta = None
last_checkin = self.last_checkin_time(user)
if last_item is not None:
if last_item.timestamp <= tz.localize(datetime.datetime.now()):
pass
if last_item.activity == "checkin":
activity = "checkout"
time_delta = last_checkin.timestamp
obj = self.model(
user=user,
activity=activity,
time_delta = time_delta,
)
obj.save()
return obj
class用户活动管理器(models.Manager):
def当前(自身、用户):
当前_obj=self.get_queryset().filter(user=user).order_by('-timestamp').first()
返回电流
def上次签入时间(自我、用户):
last_activity_time=self.get_queryset().order_by('-timestamp').filter(user=user,activity=“checkin”).first()
返回上次活动时间
def切换(自身、用户):
最后一项=自身当前(用户)
活动=“签入”
时间δ=无
上次签入=自我。上次签入时间(用户)
如果最后一项不是无:
如果last_item.timestamp而不是使用tz.localize(last_checkin.timestamp),我需要使用tz.normalize(last_checkin.timestamp)。似乎因为时间戳已经设置为UTC,我需要使用normalize方法来更改它,而不是localize我认为DB不会保存本地时间。我认为DB客户端(屏幕截图中的表)在本地时间显示它,所以我可能需要在从数据库获取时区后的某个时间设置时区?这也是我的想法,但当我尝试对值使用tz.localize(last_checkin.timestamp)时,我得到了错误“Not naive datetime(tzinfo已设置)”