18

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?

falsetru
  • 357,413
  • 63
  • 732
  • 636
DoNotArrestMe
  • 1,285
  • 1
  • 9
  • 20
  • possible duplicate of [RuntimeWarning: DateTimeField received a naive datetime](http://stackoverflow.com/questions/18622007/runtimewarning-datetimefield-received-a-naive-datetime) – falsetru Jan 10 '14 at 08:02
  • Good as an alternative [RuntimeWarning: DateTimeField received a naive datetime](http://stackoverflow.com/questions/18622007/runtimewarning-datetimefield-received-a-naive-datetime) – DoNotArrestMe Jan 10 '14 at 08:06
  • @NewInTheBusiness `now()` work well for me without warning. Thanks! – DoNotArrestMe Jan 10 '14 at 19:19
  • @Andrei Good to hear :) I don't know much about Django, but figured pure SQL should work in there. – NewInTheBusiness Jan 10 '14 at 23:54

3 Answers3

42

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())
hellsgate
  • 5,905
  • 5
  • 32
  • 47
  • I couldn't find a timezone aware alternative to datetime.datetime.combine() (to create ranges to filter DateTimeFields by date only) and this worked great – Filipe Pina Dec 16 '14 at 14:25
24

Use django.utils.timezone.now instead of datetime.datetime.now.

from django.utils import timezone
current_time = timezone.now()
falsetru
  • 357,413
  • 63
  • 732
  • 636
  • I need to do sql query directly in database. Not in django. – DoNotArrestMe Jan 10 '14 at 08:03
  • @Andrei: I don't think you do. The warning is coming from django, not the database. If you create `my_datetime` using the method described by falsetru the warning won't be generated – hellsgate Jan 10 '14 at 11:48
  • @hellsgate I know that this warning is coming from django, not from database. But my goal is insert row using sql query, not create new object using django model with default field `django.utils.timezone.now`. – DoNotArrestMe Jan 10 '14 at 12:03
  • @Andrei: Fair enough. This is still a good answer for you though as ensuring that current_time is tz aware will prevent that warning when you are building your SQL query – hellsgate Jan 10 '14 at 12:28
3

You can also make the datetime time zone aware with localize from pytz, as explained here.

UTC:

import pytz
dt_aware = pytz.utc.localize(dt_naive)

Any other time zone:

import pytz
tz = 'Europe/Berlin' #or whaterver timezone you want
dt_aware = pytz.timezone(tz).localize(dt_naive)

And here the list of timezones.

Community
  • 1
  • 1
J0ANMM
  • 7,849
  • 10
  • 56
  • 90