In Django, if you have USE_TZ = True
and pass a naive datetime to a database call, it logs the error:
DateTimeField Model.field received a naive datetime (2021-01-01 00:00:00) while time zone support is active.
However, Django goes ahead and does the operation anyway, using settings.TIME_ZONE
. Is there any way to do stricter checking? Is there a setting so that Django will raise an exception rather than just logging a warning?
The Django docs do include a way to raise an exception instead of just getting a warning (Django 4.0, have not checked how long it has been there). For me this reads as though you should only add this during development, and not for production deploy.
" Finally, in order to help you locate code that needs upgrading, Django raises a warning when you attempt to save a naive datetime to the database: 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',
)
"
https://docs.djangoproject.com/en/4.0/topics/i18n/timezones/#code