Django admin: is there an event, or another way to call a Javascript function, when a Django Admin Popup (the green plus icon) completes ?

1,059 views
Skip to first unread message

boito...@gmail.com

unread,
Nov 20, 2015, 3:55:10 AM11/20/15
to Django users
Hi,

  We are developing an application that relies on dynamic admin pages (based on some custom ajax). The high level idea is that, when the user changes the value in a select html element that is mapped to a ForeignKey on another model, we call a JS function (which triggers an asynchronous request that changes the current admin form on completion).


To achieve that, we listen to the change event of said select (to call the JS function when it is fired), which works fine when the user clicks a value directly in the drop-down menu proposed by the select.

Sadly, when this select is populated through the Admin Popup functionality (the process that starts by clicking the green plus icon to open a popup window, and that completes when the user click the save button on the popup window), it seems that the change event is not fired for the select, as our callback is not executed, even though the selected value is actually changed.

Is there another event we can listen to to get the same behaviour than when the user clicks a value directly from the list ? Or any other method to call our JS function when the select value is set through the popup completion ?

Thank you for reading,
  Ad

(Full disclosure: I first tried my luck with a question on SO, without great success)

boito...@gmail.com

unread,
Nov 25, 2015, 5:39:06 AM11/25/15
to Django users
Hi,

  I see this question is not receiving a lot of love. Perhaps we can try to approach it differently then:
 What is the mechanism that populates back the parentform select once the pop-up form is completed and saved ? (As I have very limited experience with front-end web technologies, I would not know where to look for the code implementing that functionnality).

From here, perhaps we can try to hook some custom Javascript.

Thank you for reading,
  Ad

James Murty

unread,
Nov 26, 2015, 5:58:22 AM11/26/15
to Django users
Hi,

Below is a Javascript file with the hackery we use to trigger a change event when the add/change popup window is dismissed, which works for Django 1.7 at least.

Hopefully it will help, or at least point you in the right direction.

Regards,
James

/*
 * Trigger change events when Django admin's popup window is dismissed
 */
(function($) {
    $(document).ready(function() {

        // HACK to override `dismissRelatedLookupPopup()` and
        // `dismissAddAnotherPopup()` in Django's RelatedObjectLookups.js to
        // trigger change event when an ID is selected or added via popup.
        function triggerChangeOnField(win, chosenId) {
            var name = windowname_to_id(win.name);
            var elem = document.getElementById(name);
            $(elem).change();
        }
        window.ORIGINAL_dismissRelatedLookupPopup = window.dismissRelatedLookupPopup
        window.dismissRelatedLookupPopup = function(win, chosenId) {
            ORIGINAL_dismissRelatedLookupPopup(win, chosenId);
            triggerChangeOnField(win, chosenId);
        }
        window.ORIGINAL_dismissAddAnotherPopup = window.dismissAddAnotherPopup
        window.dismissAddAnotherPopup = function(win, chosenId) {
            ORIGINAL_dismissAddAnotherPopup(win, chosenId);
            triggerChangeOnField(win, chosenId);
        }

    });
})(jQuery);

Federico Capoano

unread,
Sep 30, 2017, 12:32:51 PM9/30/17
to Django users
Hi James,

I was banging my head on the wall for this issue, you saved me some precious time, thank you!

I wonder why such a mechanism is not included by default into Django, I will bring up this issue on the development mailing list.

Federico

Federico Capoano

unread,
Sep 30, 2017, 12:40:24 PM9/30/17
to Django users
Reply all
Reply to author
Forward
0 new messages