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?

+3


source to share


2 answers


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

      

+11


source


It seems that in Django 1.11 we can simply assign an ISO format string to the DateTimeField. as shown below.



Lesson.objects.create(starttime='2017-04-20T12:01:00.000Z',
                      endtime  ='2017-04-20T13:01:00.000Z')

      

0


source







All Articles