Code

#19473 closed Uncategorized (invalid)

Getting a datetime.date 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

Description

Hi,

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 datetime.date.

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))
author.save()
book = Book(author=author)
book.save()

print repr(Book.objects.filter(pk=book.pk)[0].author.dob)
print repr(Book.objects.filter(pk=book.pk).select_related('author__dob')[0].author.dob)

I get the output

datetime.datetime(1970, 1, 1, 0, 0)
datetime.date(1970, 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.

Attachments (0)

Change History (2)

comment:1 Changed 16 months ago by mwehtam@…

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

comment:2 Changed 16 months 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.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.