Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateTimeField received a naive datetime while time zone support is active

Tags:

django

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
like image 346
fadedbee Avatar asked Jul 19 '12 08:07

fadedbee


People also ask

What is a naive datetime?

We call them "naive" because this datetime representation does not have a time zone. This means the datetime may not actually exist in certain areas in the world even though it is valid. For example, when daylight saving changes are applied by a region, the clock typically moves forward or backward by one hour.

How do I change timezone in Django?

Time zone support is disabled by default. To enable it, set USE_TZ = True in your settings file. In Django 5.0, time zone support will be enabled by default. Time zone support uses zoneinfo , which is part of the Python standard library from Python 3.9.

How can I get date field in Django?

For DateField : default=date.today - from datetime.date.today() For DateTimeField : default=timezone.now - from django.utils.timezone.now()


1 Answers

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')
like image 137
dalore Avatar answered Oct 05 '22 19:10

dalore