[英]Django Admin StackedInline Not Loading Add Another model, Uncaught TypeError: Cannot read property 'fn' of undefined
When I add a number of inlines to my django admin
as shown below the Add another {model name}
disappears. 当我向django admin
添加许多内联时,如下所示,“ Add another {model name}
消失了。 If I check the javascript console I see that I am getting the following error: 如果我检查JavaScript控制台,会看到以下错误:
Uncaught TypeError: Cannot read property 'fn' of undefined 未捕获的TypeError:无法读取未定义的属性'fn'
at inlines.js:20 在inlines.js:20
at inlines.js:295 在inlines.js:295
Here's my Django admin: 这是我的Django管理员:
@admin.register(models.Paper)
class PaperAdmin(BaseLiteratureAdmin):
class EditedPaperAdminInline(admin.StackedInline):
model = models.EditedPaper
extra = 0
class SupplementaryInformationAdminInline(admin.StackedInline):
model = models.SupplementaryInformation
extra = 0
class PaperNotesAdminInline(BaseNotesAdminInline):
exclude = tuple(
i for i in BaseNotesAdminInline.exclude if i != 'paper'
)
class ReferencedPaperInline(admin.StackedInline):
model = models.Paper.referenced_papers.through
extra = 0
fk_name = 'from_paper'
verbose_name = "Referenced Paper"
verbose_name_plural = "Referenced Papers"
inlines = (
EditedPaperAdminInline, # problem
PaperNotesAdminInline, # ok single/together
ReferencedPaperInline, # ok single/together
SupplementaryInformationAdminInline, # problem
)
Here's what I'd like to see: 这是我想看到的:
Here's what I'm seeing: 这是我所看到的:
And I found the solution--it's to change the order of the inlines in the inlines
list, like so: 我找到了解决方案-更改内inlines
列表中内inlines
的顺序,如下所示:
inlines = (
PaperNotesAdminInline, # ok single/together
ReferencedPaperInline, # ok single/together
EditedPaperAdminInline, # problem
SupplementaryInformationAdminInline, # problem
)
The #comments next to each inline note that the PaperNotesAdminInline
and ReferencedPaperInline
are both 'OK' in that the Add another {model name}
link still appears if they are included. 每个内联代码旁边的#comments指出PaperNotesAdminInline
和ReferencedPaperInline
都为“ OK”,因为如果包括它们,则“ Add another {model name}
链接仍会出现。 The other two inlines, if added in order shown in the admin model definition, result in the JavaScript error and the links disappearing for each inline. 如果按照管理模型定义中显示的顺序添加其他两个内联,则会导致JavaScript错误,并且每个内联的链接都会消失。 However, if I change the order to the second inlines
list, everything loads correctly. 但是,如果我将顺序更改为第二个内inlines
列表,则将正确加载所有内容。
What is going on here? 这里发生了什么?
I has same problem on my project, on a large rich form script inline.js loads before jQuery and produces an error. 在我的项目中,在大型富格式脚本inline.js在jQuery之前加载并产生错误时,我遇到了同样的问题。 I added changed inline.js to my project folder, it checks are jQuery loaded, and if not, it waits for 300 ms, and tries again. 我将更改的inline.js添加到我的项目文件夹中,它检查是否已加载jQuery,如果没有,则等待300毫秒,然后重试。 Patch here. 在这里打补丁。
--- django/contrib/admin/static/admin/js/inlines.js
+++ myproject/myproject/static/admin/js/inlines.js
@@ -15,7 +15,8 @@
* Licensed under the New BSD License
* See: http://www.opensource.org/licenses/bsd-license.php
*/
-(function($) {
+
+function start_inline($) {
'use strict';
$.fn.formset = function(opts) {
var options = $.extend({}, $.fn.formset.defaults, opts);
@@ -292,4 +293,16 @@
}
});
});
-})(django.jQuery);
+};
+
+function start_inline_loader() {
+ try {
+ start_inline(django.jQuery);
+
+ } catch(e) {
+ console.log('no jQuery, try again after 300 mc');
+ setTimeout( function() {start_inline_loader();}, 300)
+ }
+}
+
+start_inline_loader();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.