#36468: Popup stays blank after adding a related object when filter_horizontal 
is
used
-------------------------------------+-------------------------------------
     Reporter:  Juan Rocha           |                     Type:  Bug
       Status:  new                  |                Component:
                                     |  contrib.admin
      Version:  5.2                  |                 Severity:  Normal
     Keywords:  filter_horizontal;   |             Triage Stage:
  SelectBox; add_to_cache;           |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
 When using the "Add another" popup for a ***ForeignKey field*** in the
 Django admin and the same ModelAdmin has a ManyToManyField with
 ***filter_horizontal***, the popup window does not close properly after
 adding the related object.

 Instead, the popup remains open and white (blank), and the browser console
 throws the following error:

 {{{
 :8000/static/admin/js/SelectBox.js:60 Uncaught TypeError: Cannot read
 properties of undefined (reading 'push')
     at Object.add_to_cache (:8000/static/admin/js/SelectBox.js:60:33)
     at :8000/static/admin/j…ctLookups.js:115:31
     at NodeList.forEach (<anonymous>)
     at updateRelatedSelectsOptions
 (:8000/static/admin/j…ctLookups.js:103:24)
     at dismissAddRelatedObjectPopup
 (:8000/static/admin/j…ctLookups.js:133:17)
     at popup_response.js:12:16
 add_to_cache @ :8000/static/admin/js/SelectBox.js:60
 (anonymous) @ :8000/static/admin/j…bjectLookups.js:115
 updateRelatedSelectsOptions @ :8000/static/admin/j…bjectLookups.js:103
 dismissAddRelatedObjectPopup @ :8000/static/admin/j…bjectLookups.js:133
 (anonymous) @ popup_response.js:12
 }}}

 This seems to be caused by this block in RelatedObjectLookups.js:

 {{{
 // Update SelectBox cache for related fields.
 if (typeof SelectBox !== "undefined" && SelectBox.cache[currentSelect.id])
 {
     SelectBox.add_to_cache(select.id, option);
     SelectBox.redisplay(select.id);
 }
 }}}


 === Steps to Reproduce
 1. Register a ModelAdmin with:
   * A ForeignKey field (with an "Add another" popup)
   * A ManyToManyField using filter_horizontal

 2. Click the "Add another" button next to the ForeignKey field
 3. In the popup, add the new object and submit
 4. The popup window stays open with a white screen
 5. Console shows a JavaScript error from SelectBox.js


 === Expected Behavior
 The popup should close normally and update the original select field with
 the new object, as it does when filter_horizontal is not present.

 === Workaround
 Commenting out or removing the SelectBox.add_to_cache and
 SelectBox.redisplay lines in RelatedObjectLookups.js avoids the error and
 restores expected behavior.

 === Version
 * Django 5.2.X
 * Reproducible in the default admin interface
-- 
Ticket URL: <https://code.djangoproject.com/ticket/36468>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/django-updates/010701977ddab24d-af0e23ea-7396-456c-97e1-4d48b76ac3c8-000000%40eu-central-1.amazonses.com.

Reply via email to