Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.6.x] Fixed #19560 -- Identified field in warning for naive datetime.

Thanks gcc for the report and vajrasky for the patch.

Backport of 570d9c2 from master.
  • Loading branch information...
commit ddff6522fa72aacc7475ba5cd4bf8683ff12b8c7 1 parent 503092b
Aymeric Augustin authored October 13, 2013
10  django/db/models/fields/__init__.py
@@ -842,8 +842,9 @@ def to_python(self, value):
842 842
                 # local time. This won't work during DST change, but we can't
843 843
                 # do much about it, so we let the exceptions percolate up the
844 844
                 # call stack.
845  
-                warnings.warn("DateTimeField received a naive datetime (%s)"
846  
-                              " while time zone support is active." % value,
  845
+                warnings.warn("DateTimeField %s.%s received a naive datetime "
  846
+                              "(%s) while time zone support is active." %
  847
+                              (self.model.__name__, self.name, value),
847 848
                               RuntimeWarning)
848 849
                 default_timezone = timezone.get_default_timezone()
849 850
                 value = timezone.make_aware(value, default_timezone)
@@ -896,8 +897,9 @@ def get_prep_value(self, value):
896 897
             # For backwards compatibility, interpret naive datetimes in local
897 898
             # time. This won't work during DST change, but we can't do much
898 899
             # about it, so we let the exceptions percolate up the call stack.
899  
-            warnings.warn("DateTimeField received a naive datetime (%s)"
900  
-                          " while time zone support is active." % value,
  900
+            warnings.warn("DateTimeField %s.%s received a naive datetime (%s)"
  901
+                          " while time zone support is active." %
  902
+                          (self.model.__name__, self.name, value),
901 903
                           RuntimeWarning)
902 904
             default_timezone = timezone.get_default_timezone()
903 905
             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
@@ -275,7 +275,8 @@ def test_naive_datetime(self):
275 275
             Event.objects.create(dt=dt)
276 276
             self.assertEqual(len(recorded), 1)
277 277
             msg = str(recorded[0].message)
278  
-            self.assertTrue(msg.startswith("DateTimeField received a naive datetime"))
  278
+            self.assertTrue(msg.startswith("DateTimeField Event.dt received "
  279
+                                           "a naive datetime"))
279 280
         event = Event.objects.get()
280 281
         # naive datetimes are interpreted in local time
281 282
         self.assertEqual(event.dt, dt.replace(tzinfo=EAT))
@@ -288,7 +289,8 @@ def test_datetime_from_date(self):
288 289
             Event.objects.create(dt=dt)
289 290
             self.assertEqual(len(recorded), 1)
290 291
             msg = str(recorded[0].message)
291  
-            self.assertTrue(msg.startswith("DateTimeField received a naive datetime"))
  292
+            self.assertTrue(msg.startswith("DateTimeField Event.dt received "
  293
+                                           "a naive datetime"))
292 294
         event = Event.objects.get()
293 295
         self.assertEqual(event.dt, datetime.datetime(2011, 9, 1, tzinfo=EAT))
294 296
 
@@ -301,7 +303,8 @@ def test_naive_datetime_with_microsecond(self):
301 303
             Event.objects.create(dt=dt)
302 304
             self.assertEqual(len(recorded), 1)
303 305
             msg = str(recorded[0].message)
304  
-            self.assertTrue(msg.startswith("DateTimeField received a naive datetime"))
  306
+            self.assertTrue(msg.startswith("DateTimeField Event.dt received "
  307
+                                           "a naive datetime"))
305 308
         event = Event.objects.get()
306 309
         # naive datetimes are interpreted in local time
307 310
         self.assertEqual(event.dt, dt.replace(tzinfo=EAT))
@@ -315,7 +318,8 @@ def test_naive_datetime_with_microsecond_unsupported(self):
315 318
             Event.objects.create(dt=dt)
316 319
             self.assertEqual(len(recorded), 1)
317 320
             msg = str(recorded[0].message)
318  
-            self.assertTrue(msg.startswith("DateTimeField received a naive datetime"))
  321
+            self.assertTrue(msg.startswith("DateTimeField Event.dt received "
  322
+                                           "a naive datetime"))
319 323
         event = Event.objects.get()
320 324
         # microseconds are lost during a round-trip in the database
321 325
         # naive datetimes are interpreted in local time
@@ -402,7 +406,8 @@ def test_query_filter_with_naive_datetime(self):
402 406
             self.assertEqual(len(recorded), 3)
403 407
             for warning in recorded:
404 408
                 msg = str(warning.message)
405  
-                self.assertTrue(msg.startswith("DateTimeField received a naive datetime"))
  409
+                self.assertTrue(msg.startswith("DateTimeField Event.dt "
  410
+                                               "received a naive datetime"))
406 411
 
407 412
     @skipUnlessDBFeature('has_zoneinfo_database')
408 413
     def test_query_datetime_lookups(self):

0 notes on commit ddff652

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