Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
Malcolm Tredinnick authored
1  django/db/models/sql/subqueries.py
@@ -394,6 +394,7 @@ def add_date_select(self, field, lookup_type, order='ASC'):
394 394
         self.select = [select]
395 395
         self.select_fields = [None]
396 396
         self.select_related = False # See #7097.
  397
+        self.extra_select = {}
397 398
         self.distinct = True
398 399
         self.order_by = order == 'ASC' and [1] or [-1]
399 400
 
10  tests/regressiontests/extra_regress/models.py
... ...
@@ -1,4 +1,5 @@
1 1
 import copy
  2
+import datetime
2 3
 
3 4
 from django.contrib.auth.models import User
4 5
 from django.db import models
@@ -9,6 +10,7 @@
9 10
 class RevisionableModel(models.Model):
10 11
     base = models.ForeignKey('self', null=True)
11 12
     title = models.CharField(blank=True, max_length=255)
  13
+    when = models.DateTimeField(default=datetime.datetime.now)
12 14
 
13 15
     def __unicode__(self):
14 16
         return u"%s (%s, %s)" % (self.title, self.id, self.base.id)
@@ -31,12 +33,13 @@ class Order(models.Model):
31 33
 __test__ = {"API_TESTS": """
32 34
 # Regression tests for #7314 and #7372
33 35
 
34  
->>> rm = RevisionableModel.objects.create(title='First Revision')
  36
+>>> rm = RevisionableModel.objects.create(title='First Revision', when=datetime.datetime(2008, 9, 28, 10, 30, 0))
35 37
 >>> rm.pk, rm.base.pk
36 38
 (1, 1)
37 39
 
38 40
 >>> rm2 = rm.new_revision()
39 41
 >>> rm2.title = "Second Revision"
  42
+>>> rm.when = datetime.datetime(2008, 9, 28, 14, 25, 0)
40 43
 >>> rm2.save()
41 44
 >>> print u"%s of %s" % (rm2.title, rm2.base.title)
42 45
 Second Revision of First Revision
@@ -107,4 +110,9 @@ class Order(models.Model):
107 110
 >>> User.objects.filter(pk=u.id).extra(select={'extra_field': 1}, order_by=['extra_field']).distinct()
108 111
 [<User: fred>]
109 112
 
  113
+# When calling the dates() method on a queryset with extra selection columns,
  114
+# we can (and should) ignore those columns. They don't change the result and
  115
+# cause incorrect SQL to be produced otherwise.
  116
+>>> RevisionableModel.objects.extra(select={"the_answer": 'id'}).dates('when', 'month')
  117
+[datetime.datetime(2008, 9, 1, 0, 0)]
110 118
 """}

0 notes on commit baa9602

Please sign in to comment.
Something went wrong with that request. Please try again.