Reputation: 181
I am having the following issue on my Debian server. NOTE: This issue does not happen on local Mac env, identical codebase.
if self.ends < timezone.now():Exception Type: TypeError at /dealvote/Exception Value: can't compare offset-naive and offset-aware
I have done the following:
USE_TZ = True in settings.py TIME_ZONE = 'UTC' in settings.py Installed pytz Installed mysql timezone tables mysql_tzinfo_to_sql — (as suggested here)
Code:
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 is set with the following:
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)
Upvotes: 2
Views: 1600
Reputation: 181
Changing this line:
self.ends.replace(tzinfo=timezone.utc)
to
timezone.make_aware(self.ends, timezone.get_current_timezone())
Solved the issue!
Upvotes: 2