Skip to content

Commit

Permalink
Fixed #23420 - broken warning for unbound naive datetime objects
Browse files Browse the repository at this point in the history
Fixed issue with warning message displayed for unbound naive datetime
objects when USE_TZ is True. Adds unit test that demonstrates the issue
(discoverable when using a custom lookup in MySQL).
  • Loading branch information
chosak authored and akaariai committed Nov 3, 2014
1 parent c548c8d commit ceb1ffc
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 5 deletions.
8 changes: 6 additions & 2 deletions django/db/models/fields/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1402,9 +1402,13 @@ def get_prep_value(self, value):
# For backwards compatibility, interpret naive datetimes in local
# time. This won't work during DST change, but we can't do much
# about it, so we let the exceptions percolate up the call stack.
warnings.warn("DateTimeField %s.%s received a naive datetime (%s)"
try:
name = '%s.%s' % (self.model.__name__, self.name)
except AttributeError:
name = '(unbound)'
warnings.warn("DateTimeField %s received a naive datetime (%s)"
" while time zone support is active." %
(self.model.__name__, self.name, value),
(name, value),
RuntimeWarning)
default_timezone = timezone.get_default_timezone()
value = timezone.make_aware(value, default_timezone)
Expand Down
3 changes: 3 additions & 0 deletions docs/releases/1.7.2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,6 @@ Bugfixes

* Fixed a migrations crash when adding ``GeometryField``\s with ``blank=True``
on PostGIS (:ticket:`23731`).

* Allowed usage of ``DateTimeField()`` as ``Transform.output_field``
(:ticket:`23420`).
8 changes: 8 additions & 0 deletions tests/custom_lookups/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,11 @@ class Author(models.Model):

def __str__(self):
return self.name


@python_2_unicode_compatible
class MySQLUnixTimestamp(models.Model):
timestamp = models.PositiveIntegerField()

def __str__(self):
return self.name
34 changes: 31 additions & 3 deletions tests/custom_lookups/tests.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
from __future__ import unicode_literals

from datetime import date
from datetime import date, datetime
import time
import unittest

from django.core.exceptions import FieldError
from django.db import models
from django.db import connection
from django.test import TestCase
from .models import Author
from django.test import TestCase, override_settings
from .models import Author, MySQLUnixTimestamp


class Div3Lookup(models.Lookup):
Expand Down Expand Up @@ -172,6 +173,18 @@ def as_sql(self, qn, connection):
(lhs, rhs, lhs, rhs), params)


class DateTimeTransform(models.Transform):
lookup_name = 'as_datetime'

@property
def output_field(self):
return models.DateTimeField()

def as_sql(self, qn, connection):
lhs, params = qn.compile(self.lhs)
return 'from_unixtime({})'.format(lhs), params


class LookupTests(TestCase):
def test_basic_lookup(self):
a1 = Author.objects.create(name='a1', age=1)
Expand Down Expand Up @@ -353,6 +366,21 @@ def test_bilateral_fexpr(self):
models.IntegerField._unregister_lookup(Mult3BilateralTransform)


@override_settings(USE_TZ=True)
class DateTimeLookupTests(TestCase):
@unittest.skipUnless(connection.vendor == 'mysql', "MySQL specific SQL used")
def test_datetime_output_field(self):
models.PositiveIntegerField.register_lookup(DateTimeTransform)
try:
ut = MySQLUnixTimestamp.objects.create(timestamp=time.time())
y2k = datetime(2000, 1, 1)
self.assertQuerysetEqual(
MySQLUnixTimestamp.objects.filter(timestamp__as_datetime__gt=y2k),
[ut], lambda x: x)
finally:
models.PositiveIntegerField._unregister_lookup(DateTimeTransform)


class YearLteTests(TestCase):
def setUp(self):
models.DateField.register_lookup(YearTransform)
Expand Down

0 comments on commit ceb1ffc

Please sign in to comment.