DateTimeField received a naive datetime

DoNotArrestMe picture DoNotArrestMe · Jan 10, 2014 · Viewed 27.9k times · Source

I have model with DateTimeField column.

I'm try to insert row with database current_time value directly into table by sql query.

My sql query for MySQL database like:

INSERT INTO MyTable (..., my_datetime, ...) VALUES (..., current_time, ...)

And get:

RuntimeWarning: DateTimeField ModelName.field_name received a naive datetime (2014-01-09 22:16:23) while time zone support is active.

How to insert current time directly into table by sql query without warning?

Answer

hellsgate picture hellsgate · Jan 10, 2014

Further to falsetru's answer, if the datetime has already been created you can convert it to timezone aware:

from django.utils import timezone
my_datetime = timezone.make_aware(my_datetime, timezone.get_current_timezone())