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 cd0479ff76.
This commit is contained in:
Mark Niehues
2025-10-07 11:32:30 +02:00
committed by Jacob Walls
parent 4cecf30395
commit 08a0b92329
2 changed files with 10 additions and 1 deletions

View File

@@ -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)) {

View File

@@ -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"
)