Fixed Queryset.dates() in the presence of extra-select columns.

Any extra(select=...) columns can be ignored in the SQL for dates, since we are
only interested in extracting distinct date values. We were previously
including them by accident and it was generating incorrect SQL.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@9091 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Malcolm Tredinnick 2008-09-28 04:36:10 +00:00
parent 4eb26b1659
commit baa960209f
2 changed files with 10 additions and 1 deletions

View File

@ -394,6 +394,7 @@ class DateQuery(Query):
self.select = [select] self.select = [select]
self.select_fields = [None] self.select_fields = [None]
self.select_related = False # See #7097. self.select_related = False # See #7097.
self.extra_select = {}
self.distinct = True self.distinct = True
self.order_by = order == 'ASC' and [1] or [-1] self.order_by = order == 'ASC' and [1] or [-1]

View File

@ -1,4 +1,5 @@
import copy import copy
import datetime
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.db import models from django.db import models
@ -9,6 +10,7 @@ from django.utils.datastructures import SortedDict
class RevisionableModel(models.Model): class RevisionableModel(models.Model):
base = models.ForeignKey('self', null=True) base = models.ForeignKey('self', null=True)
title = models.CharField(blank=True, max_length=255) title = models.CharField(blank=True, max_length=255)
when = models.DateTimeField(default=datetime.datetime.now)
def __unicode__(self): def __unicode__(self):
return u"%s (%s, %s)" % (self.title, self.id, self.base.id) return u"%s (%s, %s)" % (self.title, self.id, self.base.id)
@ -31,12 +33,13 @@ class Order(models.Model):
__test__ = {"API_TESTS": """ __test__ = {"API_TESTS": """
# Regression tests for #7314 and #7372 # Regression tests for #7314 and #7372
>>> rm = RevisionableModel.objects.create(title='First Revision') >>> rm = RevisionableModel.objects.create(title='First Revision', when=datetime.datetime(2008, 9, 28, 10, 30, 0))
>>> rm.pk, rm.base.pk >>> rm.pk, rm.base.pk
(1, 1) (1, 1)
>>> rm2 = rm.new_revision() >>> rm2 = rm.new_revision()
>>> rm2.title = "Second Revision" >>> rm2.title = "Second Revision"
>>> rm.when = datetime.datetime(2008, 9, 28, 14, 25, 0)
>>> rm2.save() >>> rm2.save()
>>> print u"%s of %s" % (rm2.title, rm2.base.title) >>> print u"%s of %s" % (rm2.title, rm2.base.title)
Second Revision of First Revision Second Revision of First Revision
@ -107,4 +110,9 @@ True
>>> User.objects.filter(pk=u.id).extra(select={'extra_field': 1}, order_by=['extra_field']).distinct() >>> User.objects.filter(pk=u.id).extra(select={'extra_field': 1}, order_by=['extra_field']).distinct()
[<User: fred>] [<User: fred>]
# When calling the dates() method on a queryset with extra selection columns,
# we can (and should) ignore those columns. They don't change the result and
# cause incorrect SQL to be produced otherwise.
>>> RevisionableModel.objects.extra(select={"the_answer": 'id'}).dates('when', 'month')
[datetime.datetime(2008, 9, 1, 0, 0)]
"""} """}