Reputation: 44739
Django is giving me some runtime warnings (on code that I didn't write).
How can I get Django to give me a stacktrace, so I can see what is causing these?
/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py:808:
RuntimeWarning: DateTimeField received a naive datetime (2012-07-19 09:36:16.161479)
while time zone support is active.
RuntimeWarning
Upvotes: 15
Views: 2479
Reputation: 5834
From the docs at: https://docs.djangoproject.com/en/stable/topics/i18n/timezones/#code
During development, you can turn such warnings into exceptions and get a traceback by adding the following to your settings file:
import warnings
warnings.filterwarnings(
'error', r"DateTimeField .* received a naive datetime",
RuntimeWarning, r'django\.db\.models\.fields')
Upvotes: 22
Reputation: 174614
This means that you enabled timezone support in django; but you passed it a datetime object that doesn't have any time zone information attached to it.
If you want django's timezone support, then datetime objects used should be timezone aware.
The documentation on timezone support provides a way to turn your datetime objects into those with timzeones.
Upvotes: -2
Reputation: 53316
You can implement your a log formatter and put trace back for warning messages in the logs using traceback.print_exception()
.
Refer to Fomatter docs at FormatException
You can also refer this, How do I use Django's logger to log a traceback when I tell it to?
Upvotes: 2
Reputation: 22449
Django's source tells you what just happened:
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):
# For backwards compatibility, interpret naive datetimes in local
# time. This won't work during DST change, but we can't do much
# about it, so we let the exceptions percolate up the call stack.
warnings.warn(u"DateTimeField received a naive datetime (%s)"
u" while time zone support is active." % value,
RuntimeWarning)
default_timezone = timezone.get_default_timezone()
value = timezone.make_aware(value, default_timezone)
return value
Upvotes: -3