Opened 4 years ago

Closed 3 years ago

Last modified 3 years ago

#19560 closed Cleanup/optimization (fixed)

Please improve warning message "received a naive datetime while time zone support is active"

Reported by: Chris Wilson Owned by: nobody
Component: Database layer (models, ORM) Version: 1.4
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The warning message usually shows:

BadDataError: ('payment.Order', RuntimeWarning(u'DateTimeField received a naive datetime (2013-01-03 19:45:41.532391) while time zone support is active.',))

However it doesn't actually show which field this happened to, which makes it hard to debug without inserting breakpoints.

This three-line change to django.db.models.fields.DateTimeField:

    def get_prep_value(self, value):
        value = self.to_python(value)
        if value is not None and settings.USE_TZ and timezone.is_naive(value):
            ...
-            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)

Which results in this output instead:

DateTimeField Order.created received a naive datetime (2013-01-03 20:11:24.084774) while time zone support is active.

Attachments (1)

add_clarity_to_warning_receiving_naive_datetime.diff (5.8 KB) - added by Vajrasky Kok 3 years ago.
Added more clarity when receiving naive datetime object when timezone support is active. Fixed the doc and test as well.

Download all attachments as: .zip

Change History (8)

comment:1 Changed 4 years ago by Aymeric Augustin

Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset

The documentation explains how to turn the warnings into exceptions, providing a full backtrace.

comment:2 Changed 4 years ago by Chris Wilson

Thanks aaugustin, but that's not my question, I still want to know what field was set wrongly. Because this warning/exception is only thrown at save() time, the backtrace doesn't tell you anything about the field name.

comment:3 Changed 4 years ago by Claude Paroz

Triage Stage: UnreviewedAccepted

comment:4 in reply to:  2 Changed 4 years ago by Aymeric Augustin

Replying to gcc:

Thanks aaugustin, but that's not my question


Sorry, I wrote that comment on a phone and it was a bit short. Let's go for a longer version now that I have a keyboard :)

1) I understood your question; I just made a tangential comment without touching the triage flags.

2) Since this isn't going to be fixed until 1.6 — we're in RC for 1.5 — developers who face the same problem and find this ticket will have to resort to another debugging method. Turning warnings into exceptions is quite powerful; I hoped my comment might help developers who aren't aware of this possibility.

3) If we make that change, the part of the documentation I'm referring to won't work any more. When you're writing a patch for this ticket, don't forget to update the docs.

Changed 3 years ago by Vajrasky Kok

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

comment:5 Changed 3 years ago by Vajrasky Kok

This patch (add_clarity_to_warning_receiving_naive_datetime.diff​) is based on gcc's work (credit to him/her). It also addressed aaugustin's concern regarding the documentation on turning warning to exception.

comment:6 Changed 3 years ago by Aymeric Augustin <aymeric.augustin@…>

Resolution: fixed
Status: newclosed

In 570d9c2678d6cc564f9c70138554af8f7ce9ec49:

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

Thanks gcc for the report and vajrasky for the patch.

comment:7 Changed 3 years ago by Aymeric Augustin <aymeric.augustin@…>

In ddff6522fa72aacc7475ba5cd4bf8683ff12b8c7:

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

Thanks gcc for the report and vajrasky for the patch.

Backport of 570d9c2678d6cc564f9c70138554af8f7ce9ec49 from master.

Note: See TracTickets for help on using tickets.
Back to Top