From 7e765a68598b2b798e49bf1f4b431a7bcac085a4 Mon Sep 17 00:00:00 2001 From: Kasyap Pentamaraju Date: Sat, 25 Oct 2025 19:23:40 +0530 Subject: [PATCH 1/2] Fixed #36686 -- Clarified Meta.ordering is ignored in GROUP BY queries. --- docs/ref/models/options.txt | 6 ++++++ docs/topics/db/aggregation.txt | 11 +++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/docs/ref/models/options.txt b/docs/ref/models/options.txt index 0d27830fc93d..e961e9cdfb74 100644 --- a/docs/ref/models/options.txt +++ b/docs/ref/models/options.txt @@ -312,6 +312,12 @@ not be looking at your Django code. For example:: ordering = [F("author").asc(nulls_last=True)] +.. admonition:: Default ordering and GROUP BY + + In :ref:`GROUP BY queries ` (for example, + those using :meth:`~.QuerySet.values` and :meth:`~.QuerySet.annotate`), the + default ordering is not applied. + .. warning:: Ordering is not a free operation. Each field you add to the ordering diff --git a/docs/topics/db/aggregation.txt b/docs/topics/db/aggregation.txt index 5383ba5687e8..ba797c0b685b 100644 --- a/docs/topics/db/aggregation.txt +++ b/docs/topics/db/aggregation.txt @@ -627,8 +627,15 @@ fields you also select in a ``values()`` call. You might reasonably ask why Django doesn't remove the extraneous columns for you. The main reason is consistency with ``distinct()`` and other places: Django **never** removes ordering constraints that you have - specified (and we can't change those other methods' behavior, as that - would violate our :doc:`/misc/api-stability` policy). + specified *explicitly with* ``order_by()`` (and we can't change those + other methods' behavior, as that would violate our + :doc:`/misc/api-stability` policy). + +.. admonition:: Default ordering not applied to GROUP BY + + ``GROUP BY`` queries (for example, those using ``.values()`` and + ``.annotate()``) don't use the model's default ordering. + Use ``order_by()`` explicitly when a given order is needed. Aggregating annotations ----------------------- From 0eec2a163a4b2ea4e82c53ae1d7b71bf43ac911d Mon Sep 17 00:00:00 2001 From: Kasyap Pentamaraju Date: Tue, 11 Nov 2025 14:59:28 +0530 Subject: [PATCH 2/2] Fixed #36724 -- Removed invalid "for" attribute on tags. --- django/forms/boundfield.py | 4 +- tests/forms_tests/tests/test_forms.py | 60 ++++++++++++------- tests/forms_tests/tests/test_i18n.py | 4 +- .../widget_tests/test_clearablefileinput.py | 6 +- .../widget_tests/test_selectdatewidget.py | 2 +- tests/model_forms/tests.py | 6 +- tests/model_formsets/tests.py | 4 +- 7 files changed, 52 insertions(+), 34 deletions(-) diff --git a/django/forms/boundfield.py b/django/forms/boundfield.py index 97559fe2860a..5b8e7e5697b1 100644 --- a/django/forms/boundfield.py +++ b/django/forms/boundfield.py @@ -192,7 +192,9 @@ def label_tag(self, contents=None, attrs=None, label_suffix=None, tag=None): if id_: id_for_label = widget.id_for_label(id_) if id_for_label: - attrs = {**(attrs or {}), "for": id_for_label} + attrs = attrs or {} + if tag != "legend": + attrs = {**attrs, "for": id_for_label} if self.field.required and hasattr(self.form, "required_css_class"): attrs = attrs or {} if "class" in attrs: diff --git a/tests/forms_tests/tests/test_forms.py b/tests/forms_tests/tests/test_forms.py index 5b0c4b9a0409..8cc1fecf0a17 100644 --- a/tests/forms_tests/tests/test_forms.py +++ b/tests/forms_tests/tests/test_forms.py @@ -3913,7 +3913,7 @@ class SomeForm(Form): ) self.assertHTMLEqual( f["field"].legend_tag(), - 'Field:', + 'Field:', ) self.assertHTMLEqual( f["field"].label_tag(attrs={"class": "foo"}), @@ -3921,14 +3921,14 @@ class SomeForm(Form): ) self.assertHTMLEqual( f["field"].legend_tag(attrs={"class": "foo"}), - 'Field:', + 'Field:', ) self.assertHTMLEqual( f["field2"].label_tag(), '' ) self.assertHTMLEqual( f["field2"].legend_tag(), - 'Field2:', + "Field2:", ) def test_label_split_datetime_not_displayed(self): @@ -4190,31 +4190,47 @@ class SomeForm(Form): boundfield = SomeForm()["field"] - testcases = [ # (args, kwargs, expected) - # without anything: just print the