Answer the question
In order to leave comments, you need to log in
Django - DateTimeField - type str?
Good afternoon.
I'm using Django 1.6.
The documentation for the DateTimeField field says:
A date and time, represented in Python by a datetime.datetime instance
class Thing(models.Model):
time_start = models.DateTimeField()
logger.debug("Time start is %s" % thing.time_start.__class__.__name__)
Time start is str
Answer the question
In order to leave comments, you need to log in
There is no need to add an extra query to the database to the signal. The documentation says it right - the database does not store it as a Python datetime object, but that's not the point here, since the signal receives an instance of your class, then it should work as you described the class.
In general, your problem is not in getting the object, but in saving it! Most likely, in the time_start field you are storing a string, not a datetime object, so you get a string.
You need to save like this:
track.time_start = datetime.datetime(2013, 8, 19, 11, 28, 58)
track.save()
print "Time start is %s" % instance.time_start.__class__.__name__
>>> Time start is datetime
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question