Code

Ticket #19560: add_clarity_to_warning_receiving_naive_datetime.diff

File add_clarity_to_warning_receiving_naive_datetime.diff, 5.8 KB (added by vajrasky, 9 months ago)

Added more clarity when receiving naive datetime object when timezone support is active. Fixed the doc and test as well.

Line 
1diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py
2index 254e685..019714a 100644
3--- a/django/db/models/fields/__init__.py
4+++ b/django/db/models/fields/__init__.py
5@@ -998,8 +998,9 @@ class DateTimeField(DateField):
6                 # local time. This won't work during DST change, but we can't
7                 # do much about it, so we let the exceptions percolate up the
8                 # call stack.
9-                warnings.warn("DateTimeField received a naive datetime (%s)"
10-                              " while time zone support is active." % value,
11+                warnings.warn("DateTimeField %s.%s received a naive datetime "
12+                              "(%s) while time zone support is active." %
13+                              (self.model.__name__, self.name, value),
14                               RuntimeWarning)
15                 default_timezone = timezone.get_default_timezone()
16                 value = timezone.make_aware(value, default_timezone)
17@@ -1053,8 +1054,9 @@ class DateTimeField(DateField):
18             # For backwards compatibility, interpret naive datetimes in local
19             # time. This won't work during DST change, but we can't do much
20             # about it, so we let the exceptions percolate up the call stack.
21-            warnings.warn("DateTimeField received a naive datetime (%s)"
22-                          " while time zone support is active." % value,
23+            warnings.warn("DateTimeField %s.%s received a naive datetime (%s)"
24+                          " while time zone support is active." %
25+                          (self.model.__name__, self.name, value),
26                           RuntimeWarning)
27             default_timezone = timezone.get_default_timezone()
28             value = timezone.make_aware(value, default_timezone)
29diff --git a/docs/topics/i18n/timezones.txt b/docs/topics/i18n/timezones.txt
30index 5ed60d0..875c618 100644
31--- a/docs/topics/i18n/timezones.txt
32+++ b/docs/topics/i18n/timezones.txt
33@@ -431,15 +431,16 @@ code: :func:`~django.utils.timezone.now`,
34 Finally, in order to help you locate code that needs upgrading, Django raises
35 a warning when you attempt to save a naive datetime to the database::
36 
37-    RuntimeWarning: DateTimeField received a naive datetime (2012-01-01 00:00:00) while time zone support is active.
38+    RuntimeWarning: DateTimeField Model.field_name received a naive datetime
39+    (2012-01-01 00:00:00) while time zone support is active.
40 
41 During development, you can turn such warnings into exceptions and get a
42 traceback by adding the following to your settings file::
43 
44     import warnings
45     warnings.filterwarnings(
46-            'error', r"DateTimeField received a naive datetime",
47-            RuntimeWarning, r'django\.db\.models\.fields')
48+            'error', r"DateTimeField [\w_][\w_\d]+\.[\w_][\w_\d]+ received "
49+            "a naive datetime", RuntimeWarning, r'django\.db\.models\.fields')
50 
51 Fixtures
52 --------
53diff --git a/tests/timezones/tests.py b/tests/timezones/tests.py
54index d2f67d9..329dca7 100644
55--- a/tests/timezones/tests.py
56+++ b/tests/timezones/tests.py
57@@ -274,7 +274,8 @@ class NewDatabaseTests(TestCase):
58             Event.objects.create(dt=dt)
59             self.assertEqual(len(recorded), 1)
60             msg = str(recorded[0].message)
61-            self.assertTrue(msg.startswith("DateTimeField received a naive datetime"))
62+            self.assertTrue(msg.startswith("DateTimeField Event.dt received "
63+                                           "a naive datetime"))
64         event = Event.objects.get()
65         # naive datetimes are interpreted in local time
66         self.assertEqual(event.dt, dt.replace(tzinfo=EAT))
67@@ -287,7 +288,8 @@ class NewDatabaseTests(TestCase):
68             Event.objects.create(dt=dt)
69             self.assertEqual(len(recorded), 1)
70             msg = str(recorded[0].message)
71-            self.assertTrue(msg.startswith("DateTimeField received a naive datetime"))
72+            self.assertTrue(msg.startswith("DateTimeField Event.dt received "
73+                                           "a naive datetime"))
74         event = Event.objects.get()
75         self.assertEqual(event.dt, datetime.datetime(2011, 9, 1, tzinfo=EAT))
76 
77@@ -300,7 +302,8 @@ class NewDatabaseTests(TestCase):
78             Event.objects.create(dt=dt)
79             self.assertEqual(len(recorded), 1)
80             msg = str(recorded[0].message)
81-            self.assertTrue(msg.startswith("DateTimeField received a naive datetime"))
82+            self.assertTrue(msg.startswith("DateTimeField Event.dt received "
83+                                           "a naive datetime"))
84         event = Event.objects.get()
85         # naive datetimes are interpreted in local time
86         self.assertEqual(event.dt, dt.replace(tzinfo=EAT))
87@@ -314,7 +317,8 @@ class NewDatabaseTests(TestCase):
88             Event.objects.create(dt=dt)
89             self.assertEqual(len(recorded), 1)
90             msg = str(recorded[0].message)
91-            self.assertTrue(msg.startswith("DateTimeField received a naive datetime"))
92+            self.assertTrue(msg.startswith("DateTimeField Event.dt received "
93+                                           "a naive datetime"))
94         event = Event.objects.get()
95         # microseconds are lost during a round-trip in the database
96         # naive datetimes are interpreted in local time
97@@ -401,7 +405,8 @@ class NewDatabaseTests(TestCase):
98             self.assertEqual(len(recorded), 3)
99             for warning in recorded:
100                 msg = str(warning.message)
101-                self.assertTrue(msg.startswith("DateTimeField received a naive datetime"))
102+                self.assertTrue(msg.startswith("DateTimeField Event.dt "
103+                                               "received a naive datetime"))
104 
105     @skipUnlessDBFeature('has_zoneinfo_database')
106     def test_query_datetime_lookups(self):