30👍
Actually, the solution is hidden deeply in python docs, quote below:
When serializing an aware datetime, the UTC offset is included, like
this:
"2011-09-01T13:20:30+03:00"
Such fixtures are fully accepted, in my case it was:
"2013-03-16T17:41:28+00:00"
"2013-03-17T23:36:12+00:00"
"2013-03-18T13:19:37+00:00"
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."]
10👍
Also if you are using yaml
to serialize there seems to be a bug in unserializing datetime
s in PyYaml
:
https://code.djangoproject.com/ticket/18867
Try either using json
as serializer or you can add quotes around the datetime in the .yaml file.
- Why does python new york time zone display 4:56 instead 4:00?
- Django + virtualenv + gunicorn – No module named django.core.wsgi?
- Force delete of any previous test database (autoclobber) when running Django unit tests, eg, in PyCharm
- Python/Django "BadStatusLine" error
2👍
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
utc=pytz.UTC
# where ever you get your datetime from
unaware = datetime.datetime(2013,3,16,17,41,28,0)
now_aware = utc.localize(unaware)
- Disable caching for a view or url in django
- Why "class Meta" is necessary while creating a model form?
- How to get values of all object fields in Django?