Opened 3 years ago

Closed 3 years ago

#19473 closed Uncategorized (invalid)

Getting a object for a DateTimeField when using select_related with SQL Server

Reported by: Mat Moore Owned by: nobody
Component: Database layer (models, ORM) Version: 1.3
Severity: Normal Keywords: select_related, datetime, datetimefield, sqlserver, db
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no



When I use select_related, and the related model has a DateTimeField, I don't get a proper datetime.datetime when I then access that field. Instead I get a

If I set up the following models

from django.db import models

class Author(models.Model):
    name = models.CharField(max_length=50)
    dob = models.DateTimeField()

class Book(models.Model):
    author = models.ForeignKey(Author)

And then run the following:

from books.models import Author, Book
import datetime
author = Author(name='Bob', dob=datetime.datetime(1970, 1, 1, 0, 0, 0))
book = Book(author=author)

print repr(Book.objects.filter([0].author.dob)
print repr(Book.objects.filter('author__dob')[0].author.dob)

I get the output

datetime.datetime(1970, 1, 1, 0, 0), 1, 1)

I'm using MS SQL Server 2008 R2. I also tried running the above code with SQLLite, and in that case I get the datetime in both cases as expected.

Change History (2)

comment:1 Changed 3 years ago by mwehtam@…

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

comment:2 Changed 3 years ago by claudep

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

MS SQL is not a database supported in Django core, so you should report a bug against the MS SQL adapter. If you can reproduce with one of the supported databases (SQLite, PostgreSQL, MySQL or Oracle), then reopen.

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