#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)
Change History (8)
comment:1 by , 12 years ago
follow-up: 4 comment:2 by , 12 years ago
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 by , 12 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:4 by , 12 years ago
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.
by , 11 years ago
Attachment: | add_clarity_to_warning_receiving_naive_datetime.diff added |
---|
Added more clarity when receiving naive datetime object when timezone support is active. Fixed the doc and test as well.
comment:5 by , 11 years ago
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 by , 11 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
The documentation explains how to turn the warnings into exceptions, providing a full backtrace.