Django DateTimeField model from ISO8601 timestamp string
Django newbie here. I am creating a Django model for a SQLite database with string timestamps eg. 2014-09-23T18: 43: 26.692Z. Since I would like to end up using Django's filtering mechanism for requests (instead of using strftime), I want my model to create DateTimeField objects from ISO strings. If I try to declare DateTimeField on a raw DB field like this
startTime = models.DateTimeField(db_column='startTime')
I am getting error: object 'unicode' has no attribute 'isoformat'
I did a little work and tried the following code:
startTime = models.DateTimeField(dateutil.parser.parse(models.TextField(db_column='startTime'))
But now AttributeError error: TextField object has no 'read' attribute. What am I doing wrong? Is this even the right approach?
source to share
Since no one bothered to answer this question and it showed up at the top of google search, here to save additional search. As someone mentioned that Django's own date module is best for the job and converts ISO8601 and many other formats to Python datetime.datetime. It is also time zone aware and therefore ideal for Django projects.
from django.utils import dateparse
x = '2014-09-23T18:43:26.692Z'
y = dateparse.parse_datetime(x)
print y
And voila ..!
2014-09-23 18:43:26.692000+00:00
source to share