【问题标题】:Django Admin StackedInline Not Loading Add Another model, Uncaught TypeError: Cannot read property 'fn' of undefinedDjango Admin StackedInline Not Loading Add another model, Uncaught TypeError: Cannot read property 'fn' of undefined
【发布时间】:2018-11-15 09:25:33
【问题描述】:

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

未捕获的类型错误:无法读取未定义的属性“fn”

在 inlines.js:20

在 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
        
    )

这是我希望看到的:

这是我所看到的:

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

inlines = (
        PaperNotesAdminInline, # ok single/together
        ReferencedPaperInline, # ok single/together
        EditedPaperAdminInline, # problem
        SupplementaryInformationAdminInline, # problem
        
    )

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

这是怎么回事?

【问题讨论】:

    标签: javascript python django python-3.x


    【解决方案1】:

    我在我的项目中遇到了同样的问题,在大型丰富表单脚本 inline.js 在 jQuery 之前加载并产生错误。 我将更改的 inline.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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-24
      • 1970-01-01
      • 2013-01-15
      • 2019-02-12
      • 2021-09-20
      • 2021-06-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多