Skip to content

Commit

Permalink
[1.6.x] Fixed #21432 -- DateTimeQuery now copies tzinfo when cloning.
Browse files Browse the repository at this point in the history
Thanks Enrique Martínez for the report and @bmispelon for the tests.

Backport of 17ed99f from master.
  • Loading branch information
loic authored and bmispelon committed Nov 14, 2013
1 parent 0aa06bd commit 67c3042
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
5 changes: 5 additions & 0 deletions django/db/models/sql/subqueries.py
Expand Up @@ -269,6 +269,11 @@ class DateTimeQuery(DateQuery):


compiler = 'SQLDateTimeCompiler' 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): def _check_field(self, field):
assert isinstance(field, DateTimeField), \ assert isinstance(field, DateTimeField), \
"%r isn't a DateTimeField." % field.name "%r isn't a DateTimeField." % field.name
Expand Down
18 changes: 17 additions & 1 deletion tests/datetimes/tests.py
@@ -1,8 +1,16 @@
from __future__ import absolute_import from __future__ import absolute_import


import datetime 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 from .models import Article, Comment, Category


Expand Down Expand Up @@ -81,3 +89,11 @@ def test_related_model_traverse(self):
], ],
lambda d: d, 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)

0 comments on commit 67c3042

Please sign in to comment.