Django管理员内联“添加更多项目”按钮不起作用

时间:2018-06-12 15:28:19

标签: django python-3.x django-admin

尝试在Django中学习堆叠内联。有一个非常基本的设置

对于admin.py

from django.contrib import admin

from .models import Picture, Review


class ReviewInline(admin.StackedInline):
    model = Review
    save_on_top = True
    fields = ["reviewer"]

#@admin.register(Picture)
class PictureAdmin(admin.ModelAdmin):
    save_on_top = True
    fields = ["painter"]
    inlines = [ReviewInline,]


admin.site.register(Review)
admin.site.register(Picture, PictureAdmin)

对于models.py

from django.db import models

class Picture(models.Model):
    painter = models.CharField(("painter"), max_length=255)
    def __str__(self):
        return self.painter


class Review(models.Model):
    picture = models.ForeignKey(Picture, on_delete=models.CASCADE)
    reviewer = models.CharField( max_length=255)
    extra = 0
    def __str__(self):
        return self.reviewer

Here is a picture

可以看出,没有“添加更多项目”按钮。我认为这可能是一个JS问题,但我不确定(我确实在浏览器中启用了JS)

任何人都有任何想法?

2 个答案:

答案 0 :(得分:1)

我相信你在错误的班级中有extra=0,它应该在Inline而不是Model ......

从模型中删除extra=0

class Review(models.Model):
    picture = models.ForeignKey(Picture, on_delete=models.CASCADE)
    reviewer = models.CharField( max_length=255)

    # extra = 0     <---- remove this

    def __str__(self):
        return self.reviewer

将其添加到内联:

class ReviewInline(admin.StackedInline):
    model = Review
    save_on_top = True
    extra = 0
    fields = ["reviewer"]

理由来自this example

的代码段
@admin.register(Painter)
class PainterAdmin(admin.ModelAdmin):
    save_on_top = True
    fields = ["name"]
    inlines = [PictureInline]


class ReviewInline(admin.StackedInline):
    model = Review
    extra = 0
    fields = ["reviewer", "comment"]

编辑:第二个想到你也可能想要从内联中删除save_on_top

答案 1 :(得分:0)

清除我的Google Chrome缓存解决了这个问题!我意识到这个例子在Microsoft Edge中有效,我得到了一个线索。