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, 22 months ago)

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

  • django/db/models/fields/__init__.py

    diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py
    index 254e685..019714a 100644
    a b class DateTimeField(DateField): 
    998998                # local time. This won't work during DST change, but we can't
    999999                # do much about it, so we let the exceptions percolate up the
    10001000                # 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),
    10031004                              RuntimeWarning)
    10041005                default_timezone = timezone.get_default_timezone()
    10051006                value = timezone.make_aware(value, default_timezone)
    class DateTimeField(DateField): 
    10531054            # For backwards compatibility, interpret naive datetimes in local
    10541055            # time. This won't work during DST change, but we can't do much
    10551056            # 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),
    10581060                          RuntimeWarning)
    10591061            default_timezone = timezone.get_default_timezone()
    10601062            value = timezone.make_aware(value, default_timezone)
  • docs/topics/i18n/timezones.txt

    diff --git a/docs/topics/i18n/timezones.txt b/docs/topics/i18n/timezones.txt
    index 5ed60d0..875c618 100644
    a b code: :func:`~django.utils.timezone.now`, 
    431431Finally, in order to help you locate code that needs upgrading, Django raises
    432432a warning when you attempt to save a naive datetime to the database::
    433433
    434     RuntimeWarning: DateTimeField received a naive datetime (2012-01-01 00:00:00) while time zone support is active.
     434    RuntimeWarning: DateTimeField Model.field_name received a naive datetime
     435    (2012-01-01 00:00:00) while time zone support is active.
    435436
    436437During development, you can turn such warnings into exceptions and get a
    437438traceback by adding the following to your settings file::
    438439
    439440    import warnings
    440441    warnings.filterwarnings(
    441             'error', r"DateTimeField received a naive datetime",
    442             RuntimeWarning, r'django\.db\.models\.fields')
     442            'error', r"DateTimeField [\w_][\w_\d]+\.[\w_][\w_\d]+ received "
     443            "a naive datetime", RuntimeWarning, r'django\.db\.models\.fields')
    443444
    444445Fixtures
    445446--------
  • tests/timezones/tests.py

    diff --git a/tests/timezones/tests.py b/tests/timezones/tests.py
    index d2f67d9..329dca7 100644
    a b class NewDatabaseTests(TestCase): 
    274274            Event.objects.create(dt=dt)
    275275            self.assertEqual(len(recorded), 1)
    276276            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"))
    278279        event = Event.objects.get()
    279280        # naive datetimes are interpreted in local time
    280281        self.assertEqual(event.dt, dt.replace(tzinfo=EAT))
    class NewDatabaseTests(TestCase): 
    287288            Event.objects.create(dt=dt)
    288289            self.assertEqual(len(recorded), 1)
    289290            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"))
    291293        event = Event.objects.get()
    292294        self.assertEqual(event.dt, datetime.datetime(2011, 9, 1, tzinfo=EAT))
    293295
    class NewDatabaseTests(TestCase): 
    300302            Event.objects.create(dt=dt)
    301303            self.assertEqual(len(recorded), 1)
    302304            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"))
    304307        event = Event.objects.get()
    305308        # naive datetimes are interpreted in local time
    306309        self.assertEqual(event.dt, dt.replace(tzinfo=EAT))
    class NewDatabaseTests(TestCase): 
    314317            Event.objects.create(dt=dt)
    315318            self.assertEqual(len(recorded), 1)
    316319            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"))
    318322        event = Event.objects.get()
    319323        # microseconds are lost during a round-trip in the database
    320324        # naive datetimes are interpreted in local time
    class NewDatabaseTests(TestCase): 
    401405            self.assertEqual(len(recorded), 3)
    402406            for warning in recorded:
    403407                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"))
    405410
    406411    @skipUnlessDBFeature('has_zoneinfo_database')
    407412    def test_query_datetime_lookups(self):
Back to Top