view raw
Tadgh Tadgh - 5 months ago 34
Python Question

Timezone issue after upgrading to Django 1.9

I have a test in Django 1.8.5, which passes. Here is is

def test_user_returns_from_vacation_correctly_increments_review_timestamps(self):
self.user.profile.on_vacation = True

now =
an_hour_ago = now - timedelta(hours=1)
two_hours_ago = now - timedelta(hours=2)

self.user.profile.vacation_date = an_hour_ago = two_hours_ago
previously_studied =

rev = UserSpecific.objects.get( #<---This is the line causing the error!
self.assertNotEqual(rev.last_studied, previously_studied)
self.assertAlmostEqual(rev.last_studied, an_hour_ago, delta=timedelta(seconds=1))

However upon upgrading to Django 1.9 this test causes this error:

Traceback (most recent call last):
/pytz/", line 304, in localize
etc etc
raise ValueError('Not naive datetime (tzinfo is already set)')
ValueError: Not naive datetime (tzinfo is already set)

Here is the related

def user_returns_from_vacation(user):
Called when a user disables vacation mode. A one-time pass through their reviews in order to correct their last_studied_date, and quickly run an SRS run to determine which reviews currently need to be looked at.
""""{} has returned from vacation!".format(user.username))
vacation_date = user.profile.vacation_date
if vacation_date:
users_reviews = UserSpecific.objects.filter(user=user)
elapsed_vacation_time = - vacation_date"User {} has been gone for timedelta: {}".format(user.username, str(elapsed_vacation_time)))
updated_count = users_reviews.update(last_studied=F('last_studied') + elapsed_vacation_time)
users_reviews.update(next_review_date=F('next_review_date') + elapsed_vacation_time)"brought {} reviews out of hibernation for {}".format(updated_count, user.username))

user.profile.vacation_date = None
user.profile.on_vacation = False

UPDATE: If I remove the F() expressions, and replace them with a for loop, this error disappears. Like so:

for rev in users_reviews:
lst = rev.last_studied
nsd = rev.next_review_date
rev.last_studied = lst + elapsed_vacation_time
rev.next_review_date = nsd + elapsed_vacation_time

I noticed in the release notes for 1.9 that they had added a TIME_ZONE attribute for timezone-naive databases (like SQLite, which is what I run my tests with). I've tried adding my timezone to the DB config, but the issue persists. Any ideas? Here are my timezone-related settings in

MY_TIME_ZONE = 'America/New_York'
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),


Eventually after hassling the core developers, I found that this is indeed a bug, specifically when using SQLite. It is documented here, but is still unfixed: