[Fixed]-Django fixtures DateTimeField runtimeWarning


Actually, the solution is hidden deeply in python docs, quote below:

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


Such fixtures are fully accepted, in my case it was:


and the output was:

$ ./manage.py loaddata articles/fixtures/initial_data.json 
Installed 3 object(s) from 1 fixture(s)

Note, that '2013-03-16 17:41:28 UTC+0000' is not proper timezone aware datetime format and it will give you following error:

DeserializationError: Problem installing fixture 'articles/fixtures/initial_data.json': [u"'2013-03-16 17:41:28 UTC+0000' value has an invalid format. It must be in YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format."]


Also if you are using yaml to serialize there seems to be a bug in unserializing datetimes in PyYaml:


Try either using json as serializer or you can add quotes around the datetime in the .yaml file.


You should probably look closer at your created_at field (you do know about auto_now_add=True?).

I’m guessing at what you’re using, so you could try something like

import datetime
from django.utils.timezone import utc

Article.created_at = datetime.datetime.utcnow().replace(tzinfo=utc)

Or you could disable the timezone support by setting

USE_TZ = False

in your settings.py

Or you could make your unaware datetime aware

import datetime
import pytz

#  where ever you get your datetime from
unaware = datetime.datetime(2013,3,16,17,41,28,0)

now_aware = utc.localize(unaware)

Leave a comment