513

I m trying to send a simple mail using IPython. I have not set up any models still getting this error. What can be done?

Error : /home/sourabh/Django/learn/local/lib/python2.7/site-packages/django/db/models/fields/init.py:827: RuntimeWarning: DateTimeField received a naive datetime (2013-09-04 14:14:13.698105) while time zone support is active. RuntimeWarning)

Tried : The first step is to add USE_TZ = True to your settings file and install pytz (if possible).

Error changed:

(learn)sourabh@sL:~/Django/learn/event$ python manage.py shell
/home/sourabh/Django/learn/local/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py:53: RuntimeWarning: SQLite received a naive datetime (2013-09-05 00:59:32.181872) while time zone support is active.
  RuntimeWarning)
the
  • 21,007
  • 11
  • 68
  • 101
shifu
  • 6,586
  • 5
  • 21
  • 21

12 Answers12

711

The problem is not in Django settings, but in the date passed to the model. Here's how a timezone-aware object looks like:

>>> from django.utils import timezone
>>> import pytz
>>> timezone.now()
datetime.datetime(2013, 11, 20, 20, 8, 7, 127325, tzinfo=pytz.UTC)

And here's a naive object:

>>> from datetime import datetime
>>> datetime.now()
datetime.datetime(2013, 11, 20, 20, 9, 26, 423063)

So if you are passing email date anywhere (and it eventually gets to some model), just use Django's now(). If not, then it's probably an issue with an existing package that fetches date without timezone and you can patch the package, ignore the warning or set USE_TZ to False.

shacker
  • 14,712
  • 8
  • 89
  • 89
kravietz
  • 10,667
  • 2
  • 35
  • 27
  • 11
    Where you write `tzinfo=`, what is ``? That is not a syntactic construct I have seen. – jameshfisher Feb 13 '15 at 10:28
  • 4
    A bit late to the party, but what you are seeing is **output** from the shell. More specifically, it's the output from the datetime object's __repr__ method, that returns printable information of the object. – George Griffin Mar 09 '15 at 21:38
  • 79
    In places where you were using `datetime.now`, change it to `timezone.now`, and add `from django.utils import timezone` at the top. – Unoti Oct 21 '15 at 21:29
  • 12
    For those still looking for that part, you can use this: `import pytz` `datetime.datetime(2013, 11, 20, 20, 8, 7, 127325, tzinfo=pytz.UTC)` – Anoyz Nov 02 '16 at 16:10
  • 2
    My settings are `USE_TZ = True`, `TIME_ZONE = 'UTC'`. But when I use `timezone.now()` it doesn't show `tzinfo=`.... So this datetime object is passed as naive one. Why does it happen? – user3595632 May 09 '17 at 06:16
  • `In [3]: timezone.datetime.strptime("2017-01-01 23:11:11", '%Y-%m-%d %H:%M:%S') Out[3]: datetime.datetime(2017, 1, 1, 23, 11, 11)` – user3595632 May 09 '17 at 06:21
  • Hi, I'm trying to parse datetime string from frontend simillar to @user3595632 using `timezone.datetime.strptime`. the result is correct as expected, but I stil got naive object warning. – Adiyat Mubarak May 12 '17 at 06:11
  • @Anoyz the Django docs seem to suggest that doing that is not recommended? https://docs.djangoproject.com/en/dev/topics/i18n/timezones/#naive-and-aware-datetime-objects – wasabigeek Jun 02 '17 at 11:16
201

Use django.utils.timezone.make_aware function to make your naive datetime objects timezone aware and avoid those warnings.

It converts naive datetime object (without timezone info) to the one that has timezone info (using timezone specified in your django settings if you don't specify it explicitly as a second argument):

import datetime
from django.conf import settings
from django.utils.timezone import make_aware

naive_datetime = datetime.datetime.now()
naive_datetime.tzinfo  # None

settings.TIME_ZONE  # 'UTC'
aware_datetime = make_aware(naive_datetime)
aware_datetime.tzinfo  # <UTC>
dmrz
  • 2,243
  • 1
  • 16
  • 9
  • 2
    Thanks for this answer, it's the most django-compliant way to transform naive dates into dates with the timezone of my django settings :) – sodimel Oct 09 '19 at 15:00
  • 1
    Is it possible to put this in models.py? – Florent Jan 18 '20 at 00:18
  • @Florent there is no need to change anything in models if you use utc timezone by default, `auto_now` and `auto_now_add` will work fine for datetime fields. If you need to have timezone aware current datetime object in models for some reason - use `django.utils.timezone.now()` function. – dmrz Jan 29 '20 at 14:48
  • What if one is using it in models like ` created = models.DateTimeField(default=datetime.now)` ? How should it be done here? – MadPhysicist Oct 17 '22 at 14:45
62

Just to fix the error to set current time

from django.utils import timezone
import datetime

datetime.datetime.now(tz=timezone.utc) # you can use this value
Sachin G.
  • 1,870
  • 19
  • 24
25

Quick and dirty - Turn it off:

USE_TZ = False

in your settings.py

gies0r
  • 4,723
  • 4
  • 39
  • 50
  • 3
    When USE_TZ is True and the database supports time zones (e.g. PostgreSQL), it is an error to set this option https://docs.djangoproject.com/en/2.2/ref/settings/#time-zone – Marcaum54 Aug 02 '19 at 12:12
22

make sure settings.py has USE_TZ = True

In your python file:

from django.utils import timezone

timezone.now() # use its value in model field

Jay Soni
  • 461
  • 4
  • 6
17

One can both fix the warning and use the timezone specified in settings.py, which might be different from UTC.

For example in my settings.py I have:

USE_TZ = True
TIME_ZONE = 'Europe/Paris'

Here is a solution; the advantage is that str(mydate) gives the correct time:

>>> from datetime import datetime
>>> from django.utils.timezone import get_current_timezone
>>> mydate = datetime.now(tz=get_current_timezone())
>>> mydate
datetime.datetime(2019, 3, 10, 11, 16, 9, 184106, 
    tzinfo=<DstTzInfo 'Europe/Paris' CET+1:00:00 STD>)
>>> str(mydate)
'2019-03-10 11:16:09.184106+01:00'

Another equivalent method is using make_aware, see dmrz post.

Edouard Thiel
  • 5,878
  • 25
  • 33
  • i just followed your idea above and used an effective timezone string in my test.py. It worked flawless and i feel is even better than modifying setting.py just for this purpose. – Carlo Sep 26 '22 at 12:07
14

If you are trying to transform a naive datetime into a datetime with timezone in django, here is my solution:

>>> import datetime
>>> from django.utils import timezone
>>> t1 = datetime.datetime.strptime("2019-07-16 22:24:00", "%Y-%m-%d %H:%M:%S")
>>> t1
    datetime.datetime(2019, 7, 16, 22, 24)
>>> current_tz = timezone.get_current_timezone()
>>> t2 = current_tz.localize(t1)
>>> t2
    datetime.datetime(2019, 7, 16, 22, 24, tzinfo=<DstTzInfo 'Asia/Shanghai' CST+8:00:00 STD>)
>>>

t1 is a naive datetime and t2 is a datetime with timezone in django's settings.

hcf1425
  • 619
  • 7
  • 10
12

You can also override settings, particularly useful in tests:

from django.test import override_settings

with override_settings(USE_TZ=False):
    # Insert your code that causes the warning here
    pass

This will prevent you from seeing the warning, at the same time anything in your code that requires a timezone aware datetime may give you problems. If this is the case, see kravietz answer.

radtek
  • 34,210
  • 11
  • 144
  • 111
8

In the model, do not pass the value:

timezone.now()

Rather, remove the parenthesis, and pass:

timezone.now

If you continue to get a runtime error warning, consider changing the model field from DateTimeField to DateField.

dehidehidehi
  • 119
  • 1
  • 3
0

If you need to convert the actual date string to date object, I have got rid of the warning by simply using astimezone:

>>> from datetime import datetime, timezone
>>> datetime_str = '2013-09-04 14:14:13.698105'
>>> datetime_object = datetime.strptime(datetime_str, "%Y-%m-%d %H:%M:%S.%f")
>>> datetime_object.astimezone(timezone.utc)
datetime.datetime(2013, 9, 4, 6, 14, 13, 698105, tzinfo=datetime.timezone.utc)
codecumber
  • 102
  • 3
  • 9
0

I use function to covert date -> datetime aware format like this:

from datetime import datetime, date
from django.utils import timezone

def date_to_datetime_aware(d:date) -> datetime:
    dt = datetime.combine(d, datetime.min.time())
    dt_aware = timezone.make_aware(dt)
    return dt_aware

It helps me to awoid Django RuntimeWarning.

Gogicool
  • 121
  • 6
-1

I encountered this warning when using the following model.

from datetime import datetime

class MyObject(models.Model):
    my_date = models.DateTimeField(default=datetime.now)

To fix it, I switched to the following default.

from django.utils import timezone

class MyObject(models.Model):
    my_date = models.DateTimeField(default=timezone.now)
henyxia
  • 89
  • 1
  • 5
  • type object 'datetime.timezone' has no attribute 'now' – Lipo Feb 15 '23 at 00:33
  • Type object `datetime.timezone` has not indeed, but my solution is referring to `django.utils.timezone` which has the `now` attribute. – henyxia May 08 '23 at 19:24