简体   繁体   中英

'+add' button doesn't work properly when django-ajax-select is used

In short: django-ajax-selects works fine with filtering existing items, but gives JS error by adding new item.

Details. Using Django 3.1. At Admin site one needs to create object of model Choice with ForeignField to model Question. Django-ajax-selects ( DAS ) is used to populate the field (dynamic filtering). By typing letters DAS handles the queryset and outputs the list of relevant questions. One can choose a question from the list and save new Choice. All this works fine.

If no proper questions by typing are found then one can click +add button and add new question in popup window with form. After clicking 'Save' according to DAS docs:

  1. new question must be saved into the database;
  2. popup window must be closed;
  3. the edited field must be populated with new question .

Screenshot with popup window

The problem is that Django stops at step2: new question is created, the popup window is getting blank and doesn't close with "Popup closing ..." in the head. There is an JS error in the window:

    Uncaught TypeError: window.windowname_to_id is not a function
    at window.dismissAddRelatedObjectPopup (:8000/static/ajax_select/js/ajax_select.js:194)
    at popup_response.js:13

The HTML-code of the blank page is

<!DOCTYPE html>
<html>
  <head><title>Popup closing…</title></head>
  <body>
    <script id="django-admin-popup-response-constants"
            src="/static/admin/js/popup_response.js"
            data-popup-response="{&quot;value&quot;: &quot;6&quot;, &quot;obj&quot;: &quot;Question object (6)&quot;}">
    </script>
  </body>
</html>

Here is a piece of the JS code from ajax_select.js, where the error probably appears:

/* Called by the popup create object when it closes.
   * For the popup this is opener.dismissAddRelatedObjectPopup
   * Django implements this in RelatedObjectLookups.js
   * In django >= 1.10 we can rely on input.trigger('change')
   * and avoid this hijacking.
   */

  var djangoDismissAddRelatedObjectPopup = window.dismissAddRelatedObjectPopup || window.dismissAddAnotherPopup;
  window.dismissAddRelatedObjectPopup = function(win, newId, newRepr) {
    // Iff this is an ajax-select input then close the window and
    // trigger didAddPopup
    var name = window.windowname_to_id(win.name);

I couldn't find the function windowname_to_id() anywhere in Django dirs, but in the 10-years old ticket at djangoprojects.com, where this function was proposed for RelatedObjectLookups.js:

var uniqueness=(new Date()).valueOf();

function id_to_windowname(text) {
    text = text.replace(/\./g, '__dot__');
    text = text.replace(/\-/g, '__dash__');
    return uniqueness + text;
}

function windowname_to_id(text) {
    text = text.replace(uniqueness, '');
    text = text.replace(/__dot__/g, '.');
    text = text.replace(/__dash__/g, '-');
    return text;
}


I've tried to put barely these functions into ajax_select.js, but it didn't help. Without DAS the +add button works fine.

Any idea to solve the problem? Or may be anyone has a working example with django-ajax-selects with add function?

该问题已由 DAS 开发人员 解决,详情请参阅issue

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM