16

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.

frnhr
  • 12,354
  • 9
  • 63
  • 90
  • @PeterStahl true, it's not a real problem, as long as I don't create datetimes from shell and save them to Django models, and also do that at the "uncertain" time of the year probably close to daylight saving changes, etc. etc... :) And even so, Django would probably drop a warning. But still... Well, it doesn't look nice! :)) – frnhr Aug 22 '12 at 20:24
  • @PeterStahl maybe write that as answer... – frnhr Aug 27 '12 at 23:24
  • 1
    See https://code.djangoproject.com/ticket/19738 for Django's deliberations on what to do about this. So far no palatable solution has been presented. – Carl Meyer Jan 27 '14 at 22:48

3 Answers3

15

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)
Ekin Ertaç
  • 325
  • 3
  • 13
powlo
  • 2,538
  • 3
  • 28
  • 38
  • 3
    Great, it works perfectly! Just to be a bit more precise about where to put this code: It is enough to put it in your `settings.py` file of your Django project. You don't need to create another file named `local_settings.py` for that. – pemistahl Nov 04 '12 at 19:13
  • 1
    I had to modify this a bit to warnings.filterwarnings("ignore", category=exceptions.RuntimeWarning, module='django.db.backends.sqlite3.base', lineno=53) – yellottyellott May 22 '13 at 02:21
  • 2
    It's very important to be aware that by doing this you are exposing yourself to possible data corruption. If you write code that saves naive datetimes to your SQLite database via raw SQL queries, with this "fix" in place Django will no longer be able to warn you about that. – Carl Meyer Jan 27 '14 at 18:59
  • 1
    "There's an iceberg straight ahead!"... "Silence that man and his posterity!" – orokusaki Sep 17 '14 at 23:15
  • . . . `lineno=57` for Django 1.8 – Bob Stein Oct 07 '15 at 16:10
12

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.

pemistahl
  • 9,304
  • 8
  • 45
  • 75
-3

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

xiaket
  • 1,903
  • 1
  • 14
  • 8
  • 1
    Yeah, but I wish to use the timezone support :) – frnhr Aug 22 '12 at 19:28
  • 7
    -1. This does not solve the problem but just circumvents it by deactivating the useful and important timezone setting of Django. As of version 1.4, it's not recommended to use naive datetimes. Always use aware datetimes if you can. This will prevent you from a lot of problems later on. Also, this warning message doesn't influence IPython's behavior in any way, so it doesn't make sense to deactivate the timezone support. – pemistahl Aug 28 '12 at 08:54