Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
Aymeric Augustin authored October 13, 2013
10  django/db/models/fields/__init__.py
@@ -998,8 +998,9 @@ def to_python(self, value):
998 998
                 # local time. This won't work during DST change, but we can't
999 999
                 # do much about it, so we let the exceptions percolate up the
1000 1000
                 # call stack.
1001  
-                warnings.warn("DateTimeField received a naive datetime (%s)"
1002  
-                              " while time zone support is active." % value,
  1001
+                warnings.warn("DateTimeField %s.%s received a naive datetime "
  1002
+                              "(%s) while time zone support is active." %
  1003
+                              (self.model.__name__, self.name, value),
1003 1004
                               RuntimeWarning)
1004 1005
                 default_timezone = timezone.get_default_timezone()
1005 1006
                 value = timezone.make_aware(value, default_timezone)
@@ -1053,8 +1054,9 @@ def get_prep_value(self, value):
1053 1054
             # For backwards compatibility, interpret naive datetimes in local
1054 1055
             # time. This won't work during DST change, but we can't do much
1055 1056
             # about it, so we let the exceptions percolate up the call stack.
1056  
-            warnings.warn("DateTimeField received a naive datetime (%s)"
1057  
-                          " while time zone support is active." % value,
  1057
+            warnings.warn("DateTimeField %s.%s received a naive datetime (%s)"
  1058
+                          " while time zone support is active." %
  1059
+                          (self.model.__name__, self.name, value),
1058 1060
                           RuntimeWarning)
1059 1061
             default_timezone = timezone.get_default_timezone()
1060 1062
             value = timezone.make_aware(value, default_timezone)
5  docs/topics/i18n/timezones.txt
@@ -431,14 +431,15 @@ code: :func:`~django.utils.timezone.now`,
431 431
 Finally, in order to help you locate code that needs upgrading, Django raises
432 432
 a warning when you attempt to save a naive datetime to the database::
433 433
 
434  
-    RuntimeWarning: DateTimeField received a naive datetime (2012-01-01 00:00:00) while time zone support is active.
  434
+    RuntimeWarning: DateTimeField ModelName.field_name received a naive
  435
+    datetime (2012-01-01 00:00:00) while time zone support is active.
435 436
 
436 437
 During development, you can turn such warnings into exceptions and get a
437 438
 traceback by adding the following to your settings file::
438 439
 
439 440
     import warnings
440 441
     warnings.filterwarnings(
441  
-            'error', r"DateTimeField received a naive datetime",
  442
+            'error', r"DateTimeField .* received a naive datetime",
442 443
             RuntimeWarning, r'django\.db\.models\.fields')
443 444
 
444 445
 Fixtures
15  tests/timezones/tests.py
@@ -274,7 +274,8 @@ def test_naive_datetime(self):
274 274
             Event.objects.create(dt=dt)
275 275
             self.assertEqual(len(recorded), 1)
276 276
             msg = str(recorded[0].message)
277  
-            self.assertTrue(msg.startswith("DateTimeField received a naive datetime"))
  277
+            self.assertTrue(msg.startswith("DateTimeField Event.dt received "
  278
+                                           "a naive datetime"))
278 279
         event = Event.objects.get()
279 280
         # naive datetimes are interpreted in local time
280 281
         self.assertEqual(event.dt, dt.replace(tzinfo=EAT))
@@ -287,7 +288,8 @@ def test_datetime_from_date(self):
287 288
             Event.objects.create(dt=dt)
288 289
             self.assertEqual(len(recorded), 1)
289 290
             msg = str(recorded[0].message)
290  
-            self.assertTrue(msg.startswith("DateTimeField received a naive datetime"))
  291
+            self.assertTrue(msg.startswith("DateTimeField Event.dt received "
  292
+                                           "a naive datetime"))
291 293
         event = Event.objects.get()
292 294
         self.assertEqual(event.dt, datetime.datetime(2011, 9, 1, tzinfo=EAT))
293 295
 
@@ -300,7 +302,8 @@ def test_naive_datetime_with_microsecond(self):
300 302
             Event.objects.create(dt=dt)
301 303
             self.assertEqual(len(recorded), 1)
302 304
             msg = str(recorded[0].message)
303  
-            self.assertTrue(msg.startswith("DateTimeField received a naive datetime"))
  305
+            self.assertTrue(msg.startswith("DateTimeField Event.dt received "
  306
+                                           "a naive datetime"))
304 307
         event = Event.objects.get()
305 308
         # naive datetimes are interpreted in local time
306 309
         self.assertEqual(event.dt, dt.replace(tzinfo=EAT))
@@ -314,7 +317,8 @@ def test_naive_datetime_with_microsecond_unsupported(self):
314 317
             Event.objects.create(dt=dt)
315 318
             self.assertEqual(len(recorded), 1)
316 319
             msg = str(recorded[0].message)
317  
-            self.assertTrue(msg.startswith("DateTimeField received a naive datetime"))
  320
+            self.assertTrue(msg.startswith("DateTimeField Event.dt received "
  321
+                                           "a naive datetime"))
318 322
         event = Event.objects.get()
319 323
         # microseconds are lost during a round-trip in the database
320 324
         # naive datetimes are interpreted in local time
@@ -401,7 +405,8 @@ def test_query_filter_with_naive_datetime(self):
401 405
             self.assertEqual(len(recorded), 3)
402 406
             for warning in recorded:
403 407
                 msg = str(warning.message)
404  
-                self.assertTrue(msg.startswith("DateTimeField received a naive datetime"))
  408
+                self.assertTrue(msg.startswith("DateTimeField Event.dt "
  409
+                                               "received a naive datetime"))
405 410
 
406 411
     @skipUnlessDBFeature('has_zoneinfo_database')
407 412
     def test_query_datetime_lookups(self):

0 notes on commit 570d9c2

Please sign in to comment.
Something went wrong with that request. Please try again.