Fixed #19560 -- Identified field in warning for naive datetime.

Thanks gcc for the report and vajrasky for the patch.
This commit is contained in:
Aymeric Augustin 2013-10-13 11:54:11 +02:00
parent 00a73c1c69
commit 570d9c2678
3 changed files with 19 additions and 11 deletions

View File

@ -998,8 +998,9 @@ class DateTimeField(DateField):
# 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 received a naive datetime (%s)"
" while time zone support is active." % value,
warnings.warn("DateTimeField %s.%s received a naive datetime "
"(%s) while time zone support is active." %
(self.model.__name__, self.name, value),
RuntimeWarning)
default_timezone = timezone.get_default_timezone()
value = timezone.make_aware(value, default_timezone)
@ -1053,8 +1054,9 @@ class DateTimeField(DateField):
# 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 received a naive datetime (%s)"
" while time zone support is active." % value,
warnings.warn("DateTimeField %s.%s received a naive datetime (%s)"
" while time zone support is active." %
(self.model.__name__, self.name, value),
RuntimeWarning)
default_timezone = timezone.get_default_timezone()
value = timezone.make_aware(value, default_timezone)

View File

@ -431,14 +431,15 @@ code: :func:`~django.utils.timezone.now`,
Finally, in order to help you locate code that needs upgrading, Django raises
a warning when you attempt to save a naive datetime to the database::
RuntimeWarning: DateTimeField received a naive datetime (2012-01-01 00:00:00) while time zone support is active.
RuntimeWarning: DateTimeField ModelName.field_name received a naive
datetime (2012-01-01 00:00:00) while time zone support is active.
During development, you can turn such warnings into exceptions and get a
traceback by adding the following to your settings file::
import warnings
warnings.filterwarnings(
'error', r"DateTimeField received a naive datetime",
'error', r"DateTimeField .* received a naive datetime",
RuntimeWarning, r'django\.db\.models\.fields')
Fixtures

View File

@ -274,7 +274,8 @@ class NewDatabaseTests(TestCase):
Event.objects.create(dt=dt)
self.assertEqual(len(recorded), 1)
msg = str(recorded[0].message)
self.assertTrue(msg.startswith("DateTimeField received a naive datetime"))
self.assertTrue(msg.startswith("DateTimeField Event.dt received "
"a naive datetime"))
event = Event.objects.get()
# naive datetimes are interpreted in local time
self.assertEqual(event.dt, dt.replace(tzinfo=EAT))
@ -287,7 +288,8 @@ class NewDatabaseTests(TestCase):
Event.objects.create(dt=dt)
self.assertEqual(len(recorded), 1)
msg = str(recorded[0].message)
self.assertTrue(msg.startswith("DateTimeField received a naive datetime"))
self.assertTrue(msg.startswith("DateTimeField Event.dt received "
"a naive datetime"))
event = Event.objects.get()
self.assertEqual(event.dt, datetime.datetime(2011, 9, 1, tzinfo=EAT))
@ -300,7 +302,8 @@ class NewDatabaseTests(TestCase):
Event.objects.create(dt=dt)
self.assertEqual(len(recorded), 1)
msg = str(recorded[0].message)
self.assertTrue(msg.startswith("DateTimeField received a naive datetime"))
self.assertTrue(msg.startswith("DateTimeField Event.dt received "
"a naive datetime"))
event = Event.objects.get()
# naive datetimes are interpreted in local time
self.assertEqual(event.dt, dt.replace(tzinfo=EAT))
@ -314,7 +317,8 @@ class NewDatabaseTests(TestCase):
Event.objects.create(dt=dt)
self.assertEqual(len(recorded), 1)
msg = str(recorded[0].message)
self.assertTrue(msg.startswith("DateTimeField received a naive datetime"))
self.assertTrue(msg.startswith("DateTimeField Event.dt received "
"a naive datetime"))
event = Event.objects.get()
# microseconds are lost during a round-trip in the database
# naive datetimes are interpreted in local time
@ -401,7 +405,8 @@ class NewDatabaseTests(TestCase):
self.assertEqual(len(recorded), 3)
for warning in recorded:
msg = str(warning.message)
self.assertTrue(msg.startswith("DateTimeField received a naive datetime"))
self.assertTrue(msg.startswith("DateTimeField Event.dt "
"received a naive datetime"))
@skipUnlessDBFeature('has_zoneinfo_database')
def test_query_datetime_lookups(self):