Python 升级到Django 1.9后的时区问题

Python 升级到Django 1.9后的时区问题,python,django,timezone,django-timezone,Python,Django,Timezone,Django Timezone,我在Django 1.8.5中有一个测试,它通过了。这是 def test_user_returns_from_vacation_correctly_increments_review_timestamps(self): self.user.profile.on_vacation = True now = timezone.now() an_hour_ago = now - timedelta(hours=1) two_hours

我在Django 1.8.5中有一个测试,它通过了。这是

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

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

        self.user.profile.vacation_date = an_hour_ago
        self.user.profile.save()
        self.review.last_studied = two_hours_ago
        self.review.save()
        previously_studied = self.review.last_studied

        user_returns_from_vacation(self.user)
        rev = UserSpecific.objects.get(id=self.review.id) #<---This is the line causing the error!
        print(rev)
        self.assertNotEqual(rev.last_studied, previously_studied)
        self.assertAlmostEqual(rev.last_studied, an_hour_ago, delta=timedelta(seconds=1))
以下是相关的
用户\u从\u休假返回的\u
代码:

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.
    """
    logger.info("{} 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 = timezone.now() - vacation_date
        logger.info("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)
        logger.info("brought {} reviews out of hibernation for {}".format(updated_count, user.username))

    user.profile.vacation_date = None
    user.profile.on_vacation = False
    user.profile.save()
UPDATE:如果删除F()表达式,并将其替换为for循环,则此错误将消失。喜欢这样:

    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
        rev.save()
我在1.9版的发行说明中注意到,他们为时区原始数据库(比如SQLite,我用它运行测试)添加了一个时区属性。我已尝试将时区添加到DB配置中,但问题仍然存在。有什么想法吗?以下是我在settings.py中的时区相关设置

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

在与核心开发人员争论之后,我发现这确实是一个bug,特别是在使用SQLite时。此处有文档记录,但仍未固定:


在与核心开发人员争论之后,我发现这确实是一个bug,特别是在使用SQLite时。此处有文档记录,但仍未固定:


我可能想看看这个——我读过这个,也读过1.8版本。既然没有区别,这并不能解释为什么我的测试在一个版本中失败,而在另一个版本中失败。我可能想看看这个-我读过这个,我也读过1.8版本。既然没有区别,这并不能解释为什么我的测试在一个版本中失败,而在另一个版本中失败。
MY_TIME_ZONE = 'America/New_York'
TIME_ZONE = MY_TIME_ZONE
DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
            'TIME_ZONE': MY_TIME_ZONE
        }