在Django管理中隐藏一个内联模型

时间:2022-10-04 14:25:34

Is there a way to suppress displaying an inline template if no object exists to display in the inline? I'd rather hide the inline altogether than display an inline with blank/null values.

如果不存在要在内联中显示的对象,是否有方法抑制显示内联模板?我宁愿完全隐藏内联,也不愿显示带有空/空值的内联。

I've tried to do this two ways, first by using conditional logic to set the model attribute of InlineModelAdmin to None when the object itself is None. This doesn't work because I'm dealing with a class object in the inline definition, not instances of the class itself.

我尝试了两种方法,首先使用条件逻辑,在对象本身为None时将InlineModelAdmin的模型属性设置为None。这不起作用,因为我处理的是内联定义中的类对象,而不是类本身的实例。

My second approach has been to modify the ModelAdmin with a similar approach. Same problem.

我的第二种方法是用类似的方法修改ModelAdmin。同样的问题。

I haven't tried to modify the template, but it seems like that might be the best approach.

我还没有尝试修改模板,但是看起来这可能是最好的方法。

Modifying the queryset doesn't seem to work because the queryset is already returning an empty set.

修改queryset似乎不起作用,因为queryset已经返回一个空集。

1 个解决方案

#1


2  

Thanks to @okm

由于@okm

class MyInline(admin.TabularInline):
    model = MyModel
    extra = 0

#1


2  

Thanks to @okm

由于@okm

class MyInline(admin.TabularInline):
    model = MyModel
    extra = 0