Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -1066,6 +1066,7 @@ answer newbie questions, and generally made Django that much better:
Unai Zalakain <unai@gisa-elkartea.org>
Valentina Mukhamedzhanova <umirra@gmail.com>
valtron
Varun Kasyap Pentamaraju <varunkasyap@hotmail.com>
Vasiliy Stavenko <stavenko@gmail.com>
Vasil Vangelovski
Vibhu Agarwal <vibhu-agarwal.github.io>
Expand Down
8 changes: 7 additions & 1 deletion django/db/models/sql/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -2346,7 +2346,13 @@ def clear_ordering(self, force=False, clear_default=True):
query (not even the model's default).
"""
if not force and (
self.is_sliced or self.distinct_fields or self.select_for_update
self.is_sliced
or self.distinct_fields
or self.select_for_update
or (
isinstance(self.group_by, tuple)
and not {*self.order_by, *self.extra_order_by}.issubset(self.group_by)
)
):
return
self.order_by = ()
Expand Down
2 changes: 1 addition & 1 deletion docs/topics/i18n/translation.txt
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,7 @@ An example:
.. code-block:: html+django

{% blocktranslate count counter=list|length %}
There is only one {{ name }} object.
There is {{ counter }} {{ name }} object.
{% plural %}
There are {{ counter }} {{ name }} objects.
{% endblocktranslate %}
Expand Down
19 changes: 19 additions & 0 deletions tests/aggregation_regress/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,25 @@ def assertObjectAttrs(self, obj, **kwargs):
for attr, value in kwargs.items():
self.assertEqual(getattr(obj, attr), value)

def test_count_preserve_group_by(self):
# new release of the same book
Book.objects.create(
isbn="113235613",
name=self.b4.name,
pages=self.b4.pages,
rating=4.0,
price=Decimal("39.69"),
contact=self.a5,
publisher=self.p3,
pubdate=datetime.date(2018, 11, 3),
)
qs = Book.objects.values("contact__name", "publisher__name").annotate(
publications=Count("id")
)
self.assertEqual(qs.count(), Book.objects.count() - 1)
self.assertEqual(qs.order_by("id").count(), Book.objects.count())
self.assertEqual(qs.extra(order_by=["id"]).count(), Book.objects.count())

def test_annotation_with_value(self):
values = (
Book.objects.filter(
Expand Down