Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed #21432 -- DateTimeQuery now copies tzinfo when cloning.

Thanks Enrique Martínez for the report and @bmispelon for the tests.
  • Loading branch information...
commit 17ed99f3a3eea4bd27fa34be59c3582616ed8079 1 parent 3e845b7
@loic loic authored bmispelon committed
Showing with 22 additions and 1 deletion.
  1. +5 −0 django/db/models/sql/subqueries.py
  2. +17 −1 tests/datetimes/tests.py
View
5 django/db/models/sql/subqueries.py
@@ -260,6 +260,11 @@ class DateTimeQuery(DateQuery):
compiler = 'SQLDateTimeCompiler'
+ def clone(self, klass=None, memo=None, **kwargs):
+ if 'tzinfo' not in kwargs and hasattr(self, 'tzinfo'):
+ kwargs['tzinfo'] = self.tzinfo
+ return super(DateTimeQuery, self).clone(klass, memo, **kwargs)
+
def _check_field(self, field):
assert isinstance(field, DateTimeField), \
"%r isn't a DateTimeField." % field.name
View
18 tests/datetimes/tests.py
@@ -1,8 +1,16 @@
from __future__ import unicode_literals
import datetime
+from unittest import skipIf
+
+try:
+ import pytz
+except ImportError:
+ pytz = None
+
+from django.test import TestCase, override_settings
+from django.utils import timezone
-from django.test import TestCase
from .models import Article, Comment, Category
@@ -81,3 +89,11 @@ def test_related_model_traverse(self):
],
lambda d: d,
)
+
+ @skipIf(pytz is None, "this test requires pytz")
+ @override_settings(USE_TZ=True)
+ def test_21432(self):
+ now = timezone.localtime(timezone.now().replace(microsecond=0))
+ Article.objects.create(title="First one", pub_date=now)
+ qs = Article.objects.datetimes('pub_date', 'second')
+ self.assertEqual(qs[0], now)
Please sign in to comment.
Something went wrong with that request. Please try again.