在Django的ModelAdmin中限制编辑模式的内联

时间:2015-03-15 00:41:55

标签: django django-admin

如何限制Django的ModelAdmin内联编辑模式。这就是我尝试过的但它不起作用:

class PageAdmin(admin.ModelAdmin):
    form = PageModelForm

    def __init__(self, *args, **kwargs):
        super(PageAdmin, self).__init__(*args, **kwargs)
        if self.instance.pk:
            self.inlines = (FileInLine, )

换句话说,我想访问ModelAdmin中的实例以确定是否设置了pk,并且仅在该情况下显示内联。

1 个答案:

答案 0 :(得分:1)

试试这个

class PageAdmin(admin.ModelAdmin):
    form = PageModelForm
    inlines = [FileInLine]

    def get_formsets_with_inlines(self, request, obj=None):
        for inline in self.get_inline_instances(request, obj):
            if isinstance(inline, FileInLine) and obj is None:
                continue
            yield inline.get_formset(request, obj), inline

https://docs.djangoproject.com/en/1.7/ref/contrib/admin/#django.contrib.admin.ModelAdmin.get_formsets_with_inlines