IMFletcher IMFletcher - 2 months ago 9
Python Question

Loaddata not dealing with timestamps and timezones properly

I'm using django 1.4.1 with mysql and timezones enabled. I did a dump data to yaml, modified some fields to create some test data, and am trying to load it back in. however, Django keeps complaining about naive datetimes even though a tz is specified

specifically, my loaddata has:

fields: {created_date: !!timestamp '2012-09-15 22:17:44+00:00', ...


but loaddata gives the error:

RuntimeWarning: DateTimeField received a naive datetime (2012-09-15 22:17:44) while time zone support is active.


This doesn't make much sense to me, seeing as its:


  1. a UTC timestamp

  2. the same exact format Django exported using dumpdata



is there some way i can tell django this is a UTC date?

Aya Aya
Answer

From the docs...

When serializing an aware datetime, the UTC offset is included, like this:

"2011-09-01T13:20:30+03:00"

For a naive datetime, it obviously isn't:

"2011-09-01T13:20:30"

...so instead of...

created_date: !!timestamp '2012-09-15 22:17:44+00:00'

...either of...

created_date: '2012-09-15T22:17:44+00:00'

...or...

created_date: '2012-09-15T22:17:44Z'

...will work.

Comments