From 08a0b923298fd2489871cd88254d11f8246b8c93 Mon Sep 17 00:00:00 2001 From: Mark Niehues Date: Tue, 7 Oct 2025 11:32:30 +0200 Subject: [PATCH 1/2] Fixed #36468 -- Fixed failure to close popup when adding a related object in the admin. The issue manifested when there were multiple relations and only some of them participated in a filter_horizontal. Regression in cd0479ff764272add5e0aba2afcf5649a241ca00. --- .../admin/static/admin/js/admin/RelatedObjectLookups.js | 7 ++++++- tests/admin_views/models.py | 4 ++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/django/contrib/admin/static/admin/js/admin/RelatedObjectLookups.js b/django/contrib/admin/static/admin/js/admin/RelatedObjectLookups.js index f366f30f7e64..fb5db940d654 100644 --- a/django/contrib/admin/static/admin/js/admin/RelatedObjectLookups.js +++ b/django/contrib/admin/static/admin/js/admin/RelatedObjectLookups.js @@ -111,7 +111,12 @@ option = new Option(newRepr, newId); select.options.add(option); // Update SelectBox cache for related fields. - if (window.SelectBox !== undefined && !SelectBox.cache[currentSelect.id]) { + if ( + window.SelectBox !== undefined + && !SelectBox.cache[currentSelect.id] + // Only if SelectBox is managing that field. + && SelectBox.cache[select.id] + ) { SelectBox.add_to_cache(select.id, option); // Sort if there are any groups present if (SelectBox.cache[select.id].some(item => item.group)) { diff --git a/tests/admin_views/models.py b/tests/admin_views/models.py index fe127f57d37f..2f4e43394a03 100644 --- a/tests/admin_views/models.py +++ b/tests/admin_views/models.py @@ -1196,3 +1196,7 @@ def __str__(self): class CamelCaseRelatedModel(models.Model): m2m = models.ManyToManyField(CamelCaseModel, related_name="m2m") fk = models.ForeignKey(CamelCaseModel, on_delete=models.CASCADE, related_name="fk") + # Add another relation that will not participate in filter_horizontal. + fk2 = models.ForeignKey( + CamelCaseModel, on_delete=models.CASCADE, related_name="fk2" + ) From cbc4c9f11f32fa99ecfe2dc365b6309ee4fc9277 Mon Sep 17 00:00:00 2001 From: Jacob Walls Date: Fri, 30 Jan 2026 15:20:12 -0500 Subject: [PATCH 2/2] Fixed typos in docs/ref/models/querysets.txt. --- docs/ref/models/querysets.txt | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/docs/ref/models/querysets.txt b/docs/ref/models/querysets.txt index 042b435b902c..890395f6de21 100644 --- a/docs/ref/models/querysets.txt +++ b/docs/ref/models/querysets.txt @@ -330,9 +330,9 @@ interested in the exact number of entries, you could do this: >>> blogs = Blog.objects.alias(entries=Count("entry")).filter(entries__gt=5) ``alias()`` can be used in conjunction with :meth:`annotate`, :meth:`exclude`, -:meth:`filter`, :meth:`order_by`, and :meth:`update`. To use aliased expression -with other methods (e.g. :meth:`aggregate`), you must promote it to an -annotation:: +:meth:`filter`, :meth:`order_by`, and :meth:`update`. To use an aliased +expression with other methods (e.g. :meth:`aggregate`), you must promote it to +an annotation:: Blog.objects.alias(entries=Count("entry")).annotate( entries=F("entries"), @@ -341,9 +341,9 @@ annotation:: :meth:`filter` and :meth:`order_by` can take expressions directly, but expression construction and usage often does not happen in the same place (for example, ``QuerySet`` method creates expressions, for later use in views). -``alias()`` allows building complex expressions incrementally, possibly -spanning multiple methods and modules, refer to the expression parts by their -aliases and only use :meth:`annotate` for the final result. +``alias()`` allows building complex expressions incrementally (possibly +spanning multiple methods and modules), referring to the expression parts by +their aliases, and only using :meth:`annotate` for the final result. ``order_by()`` ~~~~~~~~~~~~~~