2
votes

I have the following model

class MonitorData(models.Model):
  [... other stuff]
  starttime = models.CharField(max_length=64, db_column='spurlStartTime', blank=True) 

So, the starttime is a char field (in the database, it represents a datetime column)

Now, here's what I am doing:

oo=models.MonitorData.objects.all()
print oo[0].starttime
print type(oo[0].starttime)

And here's what I am getting:

00:35:59
<type 'datetime.time'>

For my own reasons (don't want to discuss them) I want to read it as a varchar and then parse it myself. But django is NOT allowing me to do that because, for reasons unknown to me, it converts that to a datetime !!!

Can anybody explain to me why django has that behaviour and how can I get my precious strings back ? Is there a way to force django cast this value as string ?

ps: I know how to use str() so pls don't answer me that

2
I'm actually a little surprised this works remotely at all, but since the field is a DATETIME, Django is going to treat it as a datetime. - Chris Pratt
I'm assuming the oo[0].spurlstarttime is a typo and should be oo[0].starttime? - astevanovic
Yes it is starttime - fixed it - Serafeim
Yes, I know it is a datetime but i can't understand why i can't treat it as a string if i want to. isn't there a way to do that ? - Serafeim
Because it's not a string. If Django actually treated as a string, you'd get an IntegrityError - Chris Pratt

2 Answers

4
votes

Django hasn't done anything to it. The DBAPI adapter sees that it's a DATETIME and therefore returns a datetime. And there's nothing in CharField that forces a datetime to text.

0
votes

I think, Instead of saving the value in the datetime format. Try to save it in the isoformat. You can convert easily.