diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index 254e6854dfc..019714af6ea 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -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) diff --git a/docs/topics/i18n/timezones.txt b/docs/topics/i18n/timezones.txt index 5ed60d0a940..4652e7e9c16 100644 --- a/docs/topics/i18n/timezones.txt +++ b/docs/topics/i18n/timezones.txt @@ -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 diff --git a/tests/timezones/tests.py b/tests/timezones/tests.py index d2f67d90730..329dca7e3e4 100644 --- a/tests/timezones/tests.py +++ b/tests/timezones/tests.py @@ -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):