Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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
  • Loading branch information...
commit baa960209f4f946d333987fbabcf13d26abfeb89 1 parent 4eb26b1
@malcolmt malcolmt authored
View
1  django/db/models/sql/subqueries.py
@@ -394,6 +394,7 @@ def add_date_select(self, field, lookup_type, order='ASC'):
self.select = [select]
self.select_fields = [None]
self.select_related = False # See #7097.
+ self.extra_select = {}
self.distinct = True
self.order_by = order == 'ASC' and [1] or [-1]
View
10 tests/regressiontests/extra_regress/models.py
@@ -1,4 +1,5 @@
import copy
+import datetime
from django.contrib.auth.models import User
from django.db import models
@@ -9,6 +10,7 @@
class RevisionableModel(models.Model):
base = models.ForeignKey('self', null=True)
title = models.CharField(blank=True, max_length=255)
+ when = models.DateTimeField(default=datetime.datetime.now)
def __unicode__(self):
return u"%s (%s, %s)" % (self.title, self.id, self.base.id)
@@ -31,12 +33,13 @@ class Order(models.Model):
__test__ = {"API_TESTS": """
# 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
(1, 1)
>>> rm2 = rm.new_revision()
>>> rm2.title = "Second Revision"
+>>> rm.when = datetime.datetime(2008, 9, 28, 14, 25, 0)
>>> rm2.save()
>>> print u"%s of %s" % (rm2.title, rm2.base.title)
Second Revision of First Revision
@@ -107,4 +110,9 @@ class Order(models.Model):
>>> User.objects.filter(pk=u.id).extra(select={'extra_field': 1}, order_by=['extra_field']).distinct()
[<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)]
"""}
Please sign in to comment.
Something went wrong with that request. Please try again.