Django IPython sqlite complains about naive dateti

2019-03-12 12:28发布

I have a new project in Django 1.4, using sqlite db. Also using django_extenstions' shell_plus with no problems.

When I installed IPython, both shell and shell_plus started to complain about:

/path/to/my/virtualenv/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py:50:
RuntimeWarning: SQLite received a naive datetime (2012-07-29 13:15:45.229464) while time zone support is active.

It seems IPython itself uses unaware datetimes. How can this be fixed?

EDIT:

I don't want to disable Django's timezone support.

3条回答
我欲成王,谁敢阻挡
2楼-- · 2019-03-12 12:42

I put this in my local_settings.py:

#ignore the following error when using ipython:
#/django/db/backends/sqlite3/base.py:50: RuntimeWarning:
#SQLite received a naive datetime (2012-11-02 11:20:15.156506) while time zone support is active.

import warnings
import exceptions
warnings.filterwarnings("ignore", category=exceptions.RuntimeWarning, module='django.db.backends.sqlite3.base', lineno=53)
查看更多
你好瞎i
3楼-- · 2019-03-12 12:42

I have the same issue but I don't think that it's really a problem. IPython seems to use naive datetimes internally and Django just warns about it. Django always sends this warning when you turn on timezone support and whenever it detects a naive datetime. On my machine, this warning only appears when I start or close IPython. This warning does not influence your work with IPython in any way. So you can safely create aware datetimes normally inside IPython and also save them to your database. In order to get rid of that warning, you probably would have to work on IPython's internals.

When working with aware datetimes in general, I strongly recommend to use pytz for this purpose.

查看更多
孤傲高冷的网名
4楼-- · 2019-03-12 13:07

modify settings.py, set USE_TZ to False would solve this problem.

查看更多
登录 后发表回答