Opened 6 years ago

Closed 6 years ago

#11851 closed (duplicate)

.annotate() turns datetime fields to unicode with sqlite3

Reported by: aminc Owned by: nobody
Component: Database layer (models, ORM) Version: 1.1
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

I have two models, joined with a ManyToManyField:

class MainModel(models.Model):
    date = models.DateTimeField(auto_now_add=True)
    others = models.ManyToManyField('OtherModel')

class OtherModel(models.Model):
    pass

I set up a sample dataset:

>>> m = MainModel.objects.create()
>>> for i in range(12):
>>> ... m.others.create()

But when I use queryset annotation, my DateTimeField is giving me the raw unicode value:

>>> MainModel.objects.annotate(num_others=Count('others')).get().date
u'2009-09-08 12:54:17.380000'

>>> MainModel.objects.get().date
datetime.datetime(2009, 9, 8, 12, 54, 17, 380000)

These are my sqlite tables:

sqlite> .schema news_mainmodel
CREATE TABLE "news_mainmodel" (
    "id" integer NOT NULL PRIMARY KEY,
    "date" datetime NOT NULL
);
sqlite> .schema news_othermodel
CREATE TABLE "news_othermodel" (
    "id" integer NOT NULL PRIMARY KEY
);

Change History (2)

comment:1 Changed 6 years ago by aminc

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

Just for completeness, here is the join table. All three were created using syncdb.

sqlite> .schema news_mainmodel_others
CREATE TABLE "news_mainmodel_others" (
    "id" integer NOT NULL PRIMARY KEY,
    "mainmodel_id" integer NOT NULL REFERENCES "news_mainmodel" ("id"),
    "othermodel_id" integer NOT NULL REFERENCES "news_othermodel" ("id"),
    UNIQUE ("mainmodel_id", "othermodel_id")
);

comment:2 Changed 6 years ago by kmtracey

  • Resolution set to duplicate
  • Status changed from new to closed

This behavior is due to a bug in sqlite3, see #10031. You need to upgrade to a more recent level of sqlite.

Note: See TracTickets for help on using tickets.
Back to Top