#19473: Getting a datetime.date object for a DateTimeField when using
select_related with SQL Server
-------------------------------------+-------------------------------------
Reporter: Mat Moore | Owner: nobody
Type: Uncategorized | Status: new
Component: Database layer | Version: 1.3
(models, ORM) | Keywords: select_related,
Severity: Normal | datetime, datetimefield, sqlserver,
Triage Stage: Unreviewed | db
Easy pickings: 0 | Has patch: 0
| UI/UX: 0
-------------------------------------+-------------------------------------
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
{{{
#!python
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:
{{{
#!python
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.
--
Ticket URL: <https://code.djangoproject.com/ticket/19473>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit https://groups.google.com/groups/opt_out.