如何限制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,并且仅在该情况下显示内联。
答案 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