summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClaude Paroz <claude@2xlibre.net>2014-10-15 15:52:18 +0200
committerClaude Paroz <claude@2xlibre.net>2014-10-16 09:27:20 +0200
commit374c14b7fd8a7ba06cebfa83dd40915a50796d3e (patch)
tree3b95404984198c5ad330add30e4d50b52dde4676
parent947af46db3c285edd3679106d374b258d491ecb8 (diff)
Fixed #23659 -- Kept annotate() args ordering
Thanks Loic Bistuer and Simon Charette for the review.
-rw-r--r--django/db/models/query.py12
-rw-r--r--tests/aggregation_regress/tests.py11
2 files changed, 18 insertions, 5 deletions
diff --git a/django/db/models/query.py b/django/db/models/query.py
index 9371aa14e0..6dbca8369a 100644
--- a/django/db/models/query.py
+++ b/django/db/models/query.py
@@ -2,7 +2,7 @@
The main QuerySet implementation. This provides the public API for the ORM.
"""
-from collections import deque
+from collections import deque, OrderedDict
import copy
import sys
import warnings
@@ -786,27 +786,29 @@ class QuerySet(object):
Return a query set in which the returned objects have been annotated
with data aggregated from related fields.
"""
+ aggrs = OrderedDict() # To preserve ordering of args
for arg in args:
if arg.default_alias in kwargs:
raise ValueError("The named annotation '%s' conflicts with the "
"default name for another annotation."
% arg.default_alias)
- kwargs[arg.default_alias] = arg
+ aggrs[arg.default_alias] = arg
+ aggrs.update(kwargs)
names = getattr(self, '_fields', None)
if names is None:
names = set(self.model._meta.get_all_field_names())
- for aggregate in kwargs:
+ for aggregate in aggrs:
if aggregate in names:
raise ValueError("The annotation '%s' conflicts with a field on "
"the model." % aggregate)
obj = self._clone()
- obj._setup_aggregate_query(list(kwargs))
+ obj._setup_aggregate_query(list(aggrs))
# Add the aggregates to the query
- for (alias, aggregate_expr) in kwargs.items():
+ for (alias, aggregate_expr) in aggrs.items():
obj.query.add_aggregate(aggregate_expr, self.model, alias,
is_summary=False)
diff --git a/tests/aggregation_regress/tests.py b/tests/aggregation_regress/tests.py
index 4955573161..1e2564ea44 100644
--- a/tests/aggregation_regress/tests.py
+++ b/tests/aggregation_regress/tests.py
@@ -740,6 +740,17 @@ class AggregationTests(TestCase):
list(qs), list(Book.objects.values_list("name", flat=True))
)
+ def test_values_list_annotation_args_ordering(self):
+ """
+ Annotate *args ordering should be preserved in values_list results.
+ **kwargs comes after *args.
+ Regression test for #23659.
+ """
+ books = Book.objects.values_list("publisher__name").annotate(
+ Count("id"), Avg("price"), Avg("authors__age"), avg_pgs=Avg("pages")
+ ).order_by("-publisher__name")
+ self.assertEqual(books[0], ('Sams', 1, 23.09, 45.0, 528.0))
+
def test_annotation_disjunction(self):
qs = Book.objects.annotate(n_authors=Count("authors")).filter(
Q(n_authors=2) | Q(name="Python Web Development with Django")