diff --git a/django/contrib/admin/static/admin/js/admin/RelatedObjectLookups.js b/django/contrib/admin/static/admin/js/admin/RelatedObjectLookups.js index f366f30f7e..fb5db940d6 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 fe127f57d3..2f4e43394a 100644 --- a/tests/admin_views/models.py +++ b/tests/admin_views/models.py @@ -1196,3 +1196,7 @@ class CamelCaseModel(models.Model): 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" + )