Skip to content

Loading…

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

Closed
wants to merge 1 commit into from

2 participants

@nmfm

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.

@nmfm nmfm 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
@akaariai
Django member

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!

@akaariai akaariai closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 8, 2012
  1. @nmfm

    Fixed #18056 - Cleared aggregations on DateQuery.add_date_select

    nmfm committed
    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.
Showing with 21 additions and 0 deletions.
  1. +2 −0 django/db/models/sql/subqueries.py
  2. +19 −0 tests/modeltests/aggregation/tests.py
View
2 django/db/models/sql/subqueries.py
@@ -8,6 +8,7 @@
from django.db.models.sql.datastructures import Date
from django.db.models.sql.query import Query
from django.db.models.sql.where import AND, Constraint
+from django.utils.datastructures import SortedDict
from django.utils.functional import Promise
from django.utils.encoding import force_unicode
@@ -205,6 +206,7 @@ def add_date_select(self, field_name, lookup_type, order='ASC'):
self.select = [select]
self.select_fields = [None]
self.select_related = False # See #7097.
+ self.aggregates = SortedDict() # See 18056.
self.set_extra_mask([])
self.distinct = True
self.order_by = order == 'ASC' and [1] or [-1]
View
19 tests/modeltests/aggregation/tests.py
@@ -565,3 +565,22 @@ def test_annotate_values_list(self):
(Decimal('82.8'), 1),
]
)
+
+ def test_dates_with_aggregation(self):
+ """
+ Test that .dates() returns a distinct set of dates when applied to a QuerySet with aggregation.
+
+ Refs #18056. Previously, .dates() would return distinct (date_kind, aggregation) sets,
+ in this case (year, num_authors), so 2008 would be returned twice because there are books from
+ 2008 with a different number of authors.
+ """
+ dates = Book.objects.annotate(num_authors=Count("authors")).dates('pubdate', 'year')
+ self.assertQuerysetEqual(
+ dates, [
+ "datetime.datetime(1991, 1, 1, 0, 0)",
+ "datetime.datetime(1995, 1, 1, 0, 0)",
+ "datetime.datetime(2007, 1, 1, 0, 0)",
+ "datetime.datetime(2008, 1, 1, 0, 0)"
+ ]
+ )
+
Something went wrong with that request. Please try again.