A
A
AlexWinner2014-02-07 20:18:14
Django
AlexWinner, 2014-02-07 20:18:14

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

However, in reality it turns out:
class Thing(models.Model):
    time_start = models.DateTimeField()

Next I output:
logger.debug("Time start is %s" % thing.time_start.__class__.__name__)

I get:
Time start is str

What am I doing wrong? Why am I not getting datetime.datetime?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Valery, 2014-02-08
@AlexWinner

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()

Then it will be possible to see in the signal that the type of the object is correct:
print "Time start is %s" % instance.time_start.__class__.__name__
>>> Time start is datetime

A
alternativshik, 2014-02-07
@alternativshik

print type(thing.time_start)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question