'+add'按钮在使用 django-ajax-select 时无法正常工作



简而言之:django-ajax选择可以很好地过滤现有项目,但添加新项目会给JS带来错误。

详细信息。使用Django 3.1。在Admin站点,需要创建模型Choice的对象和ForeignField来建模Question。Django-ajax-selects(DAS(用于填充字段(动态过滤(。DAS通过键入字母来处理查询集并输出相关问题的列表。可以从列表中选择一个问题,然后保存新的"选项"。所有这些都很好。

如果没有通过打字找到合适的问题,那么可以点击+添加按钮,并在弹出窗口中添加新的问题。根据DAS文档点击"保存"后:

  1. 新问题必须保存到数据库中
  2. 弹出窗口必须关闭
  3. 编辑后的字段必须填充新的问题

带有弹出窗口的屏幕截图

问题是Django在第2步停止了:创建了新的问题,弹出窗口变为空白,并且没有以"弹出式关闭"头部。窗口中出现JS错误:

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

空白页的HTML代码是

<!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>

以下是ajax_select.JS中的一段JS代码,其中可能出现错误:

/* 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);

我在Django目录中找不到函数windowname_to_id((,但在djangoprojects.com上有10年历史的票证中,这个函数是为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;
}

我试着把这些函数放进ajax_select.js中,但无济于事。没有DAS,+添加按钮工作正常。

有解决问题的办法吗?或者可能有人有一个使用django-ajax的工作示例,用add函数进行选择?

问题由DAS开发人员解决,有关详细信息,请参阅问题。

最新更新