【发布时间】:2026-01-18 04:40:01
【问题描述】:
我在我的 Debian 服务器上遇到以下问题。注意:在本地 Mac 环境、相同的代码库上不会发生此问题。
if self.ends < timezone.now():Exception Type: TypeError at /dealvote/Exception Value: can't compare offset-naive and offset-aware
我做了以下事情:
USE_TZ = 在 settings.py 中为真 TIME_ZONE = settings.py 中的“UTC” 安装 pytz 已安装 mysql 时区表 mysql_tzinfo_to_sql —(如建议此处)
代码:
ends = models.DateTimeField(blank=True)
@property
def isEnded(self):
if self.ends is not None:
#Extra check to make sure self.ends is aware
if timezone.is_naive(self.ends):
self.ends.replace(tzinfo=timezone.utc)
#self.ends is not aware here. timezone.now() is aware as checked in django shell
if self.ends < timezone.now():
return True
else:
return False
ends 设置如下:
def setEnd(self, mins, hrs, dys):
self.ends = timezone.now()
isFlashDeal = False
try:
minutes = int(mins)
self.ends += timedelta(minutes=int(mins))
isFlashDeal = True
except Exception:
pass
try:
self.ends += timedelta(hours=int(hrs))
isFlashDeal = True
except Exception:
pass
try:
self.ends += timedelta(days=int(dys))
isFlashDeal = True
except Exception:
pass
if isFlashDeal == False:
self.ends = None
if timezone.is_naive(self.ends):
self.ends.replace(tzinfo=timezone.utc)
【问题讨论】:
-
您应该将解决方案作为您自己问题的答案(并保持您的问题原样),以帮助将来遇到同样问题的人。
-
现在完成。当时它不会让我,因为我的代表太低了。