Author: mtredinnick
Date: 2008-09-27 23:36:10 -0500 (Sat, 27 Sep 2008)
New Revision: 9091

Modified:
   django/trunk/django/db/models/sql/subqueries.py
   django/trunk/tests/regressiontests/extra_regress/models.py
Log:
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.


Modified: django/trunk/django/db/models/sql/subqueries.py
===================================================================
--- django/trunk/django/db/models/sql/subqueries.py     2008-09-28 03:08:30 UTC 
(rev 9090)
+++ django/trunk/django/db/models/sql/subqueries.py     2008-09-28 04:36:10 UTC 
(rev 9091)
@@ -394,6 +394,7 @@
         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]
 

Modified: django/trunk/tests/regressiontests/extra_regress/models.py
===================================================================
--- django/trunk/tests/regressiontests/extra_regress/models.py  2008-09-28 
03:08:30 UTC (rev 9090)
+++ django/trunk/tests/regressiontests/extra_regress/models.py  2008-09-28 
04:36:10 UTC (rev 9091)
@@ -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 @@
 __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 @@
 >>> 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)]
 """}


--~--~---------~--~----~------------~-------~--~----~
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 this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to