From ade52ef71f04e57e217585358cb289098260e3ec Mon Sep 17 00:00:00 2001 From: Andrew Nester Date: Tue, 29 Nov 2016 20:23:44 +0300 Subject: [PATCH] Fixed #27544 -- Fixed QuerySet.update(dt=F('dt') + timedelta) crash on SQLite. --- django/db/backends/sqlite3/operations.py | 2 +- docs/releases/1.10.4.txt | 4 ++++ tests/timezones/tests.py | 10 +++++++++- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/django/db/backends/sqlite3/operations.py b/django/db/backends/sqlite3/operations.py index ad6b2a5a2a..1ab20767c4 100644 --- a/django/db/backends/sqlite3/operations.py +++ b/django/db/backends/sqlite3/operations.py @@ -217,7 +217,7 @@ class DatabaseOperations(BaseDatabaseOperations): if value is not None: if not isinstance(value, datetime.datetime): value = parse_datetime(value) - if settings.USE_TZ: + if settings.USE_TZ and not timezone.is_aware(value): value = timezone.make_aware(value, self.connection.timezone) return value diff --git a/docs/releases/1.10.4.txt b/docs/releases/1.10.4.txt index 4d3ad91cf4..0f1f70e95d 100644 --- a/docs/releases/1.10.4.txt +++ b/docs/releases/1.10.4.txt @@ -19,3 +19,7 @@ Bugfixes * Made ``Model.delete(keep_parents=True)`` preserve parent reverse relationships in multi-table inheritance (:ticket:`27407`). + +* Fixed a ``QuerySet.update()`` crash on SQLite when updating a + ``DateTimeField`` with an ``F()`` expression and a ``timedelta`` + (:ticket:`27544`). diff --git a/tests/timezones/tests.py b/tests/timezones/tests.py index d95877d227..c50d356074 100644 --- a/tests/timezones/tests.py +++ b/tests/timezones/tests.py @@ -14,7 +14,7 @@ from django.contrib.auth.models import User from django.core import serializers from django.core.exceptions import ImproperlyConfigured from django.db import connection, connections -from django.db.models import Max, Min +from django.db.models import F, Max, Min from django.http import HttpRequest from django.template import ( Context, RequestContext, Template, TemplateSyntaxError, context_processors, @@ -26,6 +26,7 @@ from django.test import ( from django.test.utils import requires_tz_support from django.urls import reverse from django.utils import six, timezone +from django.utils.timezone import timedelta from .forms import ( EventForm, EventLocalizedForm, EventLocalizedModelForm, EventModelForm, @@ -569,6 +570,13 @@ class NewDatabaseTests(TestCase): e = MaybeEvent.objects.create() self.assertIsNone(e.dt) + def test_update_with_timedelta(self): + initial_dt = timezone.now().replace(microsecond=0) + event = Event.objects.create(dt=initial_dt) + Event.objects.update(dt=F('dt') + timedelta(hours=2)) + event.refresh_from_db() + self.assertEqual(event.dt, initial_dt + timedelta(hours=2)) + @override_settings(TIME_ZONE='Africa/Nairobi', USE_TZ=True) class ForcedTimeZoneDatabaseTests(TransactionTestCase):