Django Admin StackedLine未加载添加其他模型,未捕获类型错误:无法读取未定义的属性“fn”

2024-05-15 03:29:06 发布

您现在位置:Python中文网/ 问答频道 /正文

当我向django admin添加多个内联时,Add another {model name}将消失。如果我检查javascript控制台,我会看到以下错误:

Uncaught TypeError: Cannot read property 'fn' of undefined

at inlines.js:20

at inlines.js:295

这是我的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

    )

我想看看: Admin with ability to Add new model

我看到的是: enter image description here

我找到了一个解决方案——改变inlines列表中内联线的顺序,如下所示:

^{pr2}$

每个内联旁边的注释注意到PaperNotesAdminInline和{}都是“正常”的,因为如果包含{}链接,它们仍然会出现。如果按照管理模型定义中显示的顺序添加其他两个内联,则会导致JavaScript错误,并且每个内联的链接都会消失。但是,如果我将顺序更改为第二个inlines列表,则所有内容都会正确加载。在

这是怎么回事?在


Tags: namemodeladmin顺序models错误jsextra
1条回答
网友
1楼 · 发布于 2024-05-15 03:29:06

我在我的项目中也遇到了同样的问题,在一个大型的富表单脚本上内联.js在jQuery之前加载并生成错误。 我加了个零钱内联.js在我的项目文件夹中,它会检查jQuery是否已加载,如果没有,它将等待300毫秒,然后重试。 贴在这里。在

 - 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();

相关问题 更多 >

    热门问题