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/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 ----------------------- 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