0
votes

In my settings.py TIME_ZONE is set to 'UTC'.

In one of my models I'm importing the created_on field from an external API which returns time in utc format. For eg: 1515374422.0 To convert that into DateTime format I use:

created_on=datetime.datetime.fromtimestamp(float(1515374422.0))
post=Post(name="ABC", created_on=created_on)

But that always runs with a RunTime warning of:

RuntimeWarning: DateTimeField Image.added_on received a naive datetime (2017-12-14 14:48:22) while time zone support is active.

I don't understand it. What does that mean? Is something wrong with the DateTime conversion code?

1

1 Answers

1
votes

The short answer is that the django orm expects all datetime objects to have the timezone set. For your code, since you know the timezone is coming in as UTC, you can simply do the following:

import pytz
created_on=datetime.datetime.fromtimestamp(float(1515374422.0))
created_on = created_on.replace(tzinfo=pytz.utc)
post=Post(name="ABC", created_on=created_on)

The additional line will add the timezone information for created on so that you are explicitly asking for a datetime in UTC.