Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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

Thanks gcc for the report and vajrasky for the patch.
  • Loading branch information...
commit 570d9c2678d6cc564f9c70138554af8f7ce9ec49 1 parent 00a73c1
@aaugustin aaugustin authored
View
10 django/db/models/fields/__init__.py
@@ -998,8 +998,9 @@ def to_python(self, value):
# 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 @@ 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 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
5 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
View
15 tests/timezones/tests.py
@@ -274,7 +274,8 @@ def test_naive_datetime(self):
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 @@ def test_datetime_from_date(self):
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 @@ def test_naive_datetime_with_microsecond(self):
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 @@ def test_naive_datetime_with_microsecond_unsupported(self):
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 @@ def test_query_filter_with_naive_datetime(self):
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):
Please sign in to comment.
Something went wrong with that request. Please try again.