Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Fixed #18056 - Cleared aggregations on DateQuery.add_date_select #132

Closed
wants to merge 1 commit into from

2 participants

Nuno Maltez Anssi Kääriäinen
Nuno Maltez
nmfm commented June 08, 2012

Cleared aggregations on add_date_select method so only distinct dates are returned when dealing
with a QuerySet that contained aggregations. That would cause the query set to return repeated
dates because it would look for distinct (date kind, aggregation) pairs.

Tests pass on sqllite.

Nuno Maltez Fixed #18056 - Cleared aggregations on DateQuery.add_date_select
Cleared aggregations on add_date_select method so only distinct dates are returned when dealing
with a QuerySet that contained aggregations. That would cause the query set to return repeated
dates because it would look for distinct (date kind, aggregation) pairs.
f5a7a64
Anssi Kääriäinen
Owner

Pushed in manually in commit bebbbb7

A few minor notes for future:

  • check for whitespace errors (git diff --check is very useful here).
  • avoid docstrings containing lines longer than 80 chars.

Thanks for the patch!

Anssi Kääriäinen akaariai closed this July 16, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Jun 08, 2012
Nuno Maltez Fixed #18056 - Cleared aggregations on DateQuery.add_date_select
Cleared aggregations on add_date_select method so only distinct dates are returned when dealing
with a QuerySet that contained aggregations. That would cause the query set to return repeated
dates because it would look for distinct (date kind, aggregation) pairs.
f5a7a64
This page is out of date. Refresh to see the latest.
2  django/db/models/sql/subqueries.py
@@ -8,6 +8,7 @@
8 8
 from django.db.models.sql.datastructures import Date
9 9
 from django.db.models.sql.query import Query
10 10
 from django.db.models.sql.where import AND, Constraint
  11
+from django.utils.datastructures import SortedDict
11 12
 from django.utils.functional import Promise
12 13
 from django.utils.encoding import force_unicode
13 14
 
@@ -205,6 +206,7 @@ def add_date_select(self, field_name, lookup_type, order='ASC'):
205 206
         self.select = [select]
206 207
         self.select_fields = [None]
207 208
         self.select_related = False # See #7097.
  209
+        self.aggregates = SortedDict() # See 18056.
208 210
         self.set_extra_mask([])
209 211
         self.distinct = True
210 212
         self.order_by = order == 'ASC' and [1] or [-1]
19  tests/modeltests/aggregation/tests.py
@@ -565,3 +565,22 @@ def test_annotate_values_list(self):
565 565
                 (Decimal('82.8'), 1),
566 566
             ]
567 567
         )
  568
+
  569
+    def test_dates_with_aggregation(self):
  570
+        """
  571
+        Test that .dates() returns a distinct set of dates when applied to a QuerySet with aggregation.
  572
+        
  573
+        Refs #18056. Previously, .dates() would return distinct (date_kind, aggregation) sets, 
  574
+        in this case (year, num_authors), so 2008 would be returned twice because there are books from
  575
+        2008 with a different number of authors.
  576
+        """
  577
+        dates = Book.objects.annotate(num_authors=Count("authors")).dates('pubdate', 'year')
  578
+        self.assertQuerysetEqual(
  579
+            dates, [
  580
+                "datetime.datetime(1991, 1, 1, 0, 0)",
  581
+                "datetime.datetime(1995, 1, 1, 0, 0)",
  582
+                "datetime.datetime(2007, 1, 1, 0, 0)",
  583
+                "datetime.datetime(2008, 1, 1, 0, 0)"
  584
+            ]
  585
+        )
  586
+        
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.