Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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 Bistuer authored November 14, 2013 bmispelon committed November 14, 2013
5  django/db/models/sql/subqueries.py
@@ -260,6 +260,11 @@ class DateTimeQuery(DateQuery):
260 260
 
261 261
     compiler = 'SQLDateTimeCompiler'
262 262
 
  263
+    def clone(self, klass=None, memo=None, **kwargs):
  264
+        if 'tzinfo' not in kwargs and hasattr(self, 'tzinfo'):
  265
+            kwargs['tzinfo'] = self.tzinfo
  266
+        return super(DateTimeQuery, self).clone(klass, memo, **kwargs)
  267
+
263 268
     def _check_field(self, field):
264 269
         assert isinstance(field, DateTimeField), \
265 270
             "%r isn't a DateTimeField." % field.name
18  tests/datetimes/tests.py
... ...
@@ -1,8 +1,16 @@
1 1
 from __future__ import unicode_literals
2 2
 
3 3
 import datetime
  4
+from unittest import skipIf
  5
+
  6
+try:
  7
+    import pytz
  8
+except ImportError:
  9
+    pytz = None
  10
+
  11
+from django.test import TestCase, override_settings
  12
+from django.utils import timezone
4 13
 
5  
-from django.test import TestCase
6 14
 
7 15
 from .models import Article, Comment, Category
8 16
 
@@ -81,3 +89,11 @@ def test_related_model_traverse(self):
81 89
             ],
82 90
             lambda d: d,
83 91
         )
  92
+
  93
+    @skipIf(pytz is None, "this test requires pytz")
  94
+    @override_settings(USE_TZ=True)
  95
+    def test_21432(self):
  96
+        now = timezone.localtime(timezone.now().replace(microsecond=0))
  97
+        Article.objects.create(title="First one", pub_date=now)
  98
+        qs = Article.objects.datetimes('pub_date', 'second')
  99
+        self.assertEqual(qs[0], now)

0 notes on commit 17ed99f

Please sign in to comment.
Something went wrong with that request. Please try again.