Elektito
Elektito

Reputation: 4155

Django datetime field (with sqlite database) returns unicode

I'm writing a small Django app that displays a few simple values from an sqlite3 database. My problem is, although one of the fields is a DateTimeField, I get a unicode value for that field when querying. Here's the relevant part of the model:

class Totals(models.Model):
    time = models.DateTimeField(primary_key=True, blank=False)
    ..

Now if I for example type Totals.objects.all()[0].time in the Django shell (after importing Totals of course), I get u'2012-01-03 04:02:56.966'. Is this normal behavior perhaps, since I'm using sqlite, or is there something wrong? I'm not sure this is relevant, but I had the models auto-generated since I already had the database I wanted to use. The auto-generation procedure naturally had deducted all field types as text, so I fixed them accordingly.

Upvotes: 2

Views: 3945

Answers (1)

jcollado
jcollado

Reputation: 40384

For what is worth, I'm not getting that behaviour (I just get a datetime.datetime object back) using sqlite and I defined the model exactly in the same way you as you provided in the question.

My guess is that there was some problem during the auto-generation of the models and they don't really match the schema in the database. Hence, I'd recommend to verify that the schema that would be generated from those models actually matches.

In my case, what I see is as follows:

$ python manage.py sql <appname>
...
CREATE TABLE "<appname>_totals" (
"time" datetime NOT NULL PRIMARY KEY
)
;


$ sqlite3 <database_filename>
.schema <app_name>_totals
CREATE TABLE "<app_name>_totals" (
"time" datetime NOT NULL PRIMARY KEY
);

Note: I'm also using Ubuntu 11.10 with python 2.7.2 and django 1.3.0

Upvotes: 2

Related Questions