问题描述
给出一个名为 MainModel
的模型和一个 RelatedModel
,其中后者具有 MainModel
的 ForeignKey
字段:
Given a model named MainModel
and a RelatedModel
, where the later has a ForeignKey
field to MainModel
:
class MainModel(models.Model):
name = models.CharField(max_length=50)
type = models.BooleanField()
class RelatedModel1(models.Model):
main = models.ForeingKey(MainModel):
name = models.CharField(max_length=50)
class RelatedModel2(models.Model):
main = models.ForeingKey(MainModel):
name = models.CharField(max_length=50)
和相应的ModelAdmin类:
and the corresponding ModelAdmin classes:
class RelatedModel1InlineAdmin(admin.TabularInline):
model = RelatedModel1
class RelatedModel2InlineAdmin(admin.TabularInline):
model = RelatedModel2
class MainModel(admin.ModelAdmin):
inlines = [RelatedModel1, RelatedModel2]
这是默认行为,您将获得两个内联,每个相关模型一个.问题是如何在创建 MainModel
实例( ModelAdmin
的 add_view
)时完全隐藏所有内联,并显示当 MainModel
实例的 type
字段为 True
时, RelatedModel1
的内联,并显示的内联当
. False
时为RelatedModel2
And that's the default behavior, you get two inlines, one for every related model. The question is how to hide completely all the inlines when the MainModel
instance is being created (the ModelAdmin
's add_view
), and to show the inlines for RelatedModel1
when the type
field of the MainModel
instance is True
, and show the inlines for RelatedModel2
when False
.
我打算为 ModelAdmin.inline_instances
创建一个描述符属性,但我意识到我需要访问正在编辑的对象实例,但它会作为参数传递.
I was going to create a descriptor for the ModelAdmin.inline_instances
attribute, but I realized that I need access to the object instance being edited, but it is passed around as parameters.
有帮助吗?
谢谢!
推荐答案
@Yuji'Tomita'Tomitayou这个主意很好,我也有同样的想法,但是一旦尝试,我意识到您还必须从 self上删除特定的密钥.内联
,因为在 change_view
和 add_view
方法中,在 get_formsets()
之前调用方法 self.get_inline_instances(request)
>.因此,我将内联处理移到了 get_form()
方法.
@Yuji 'Tomita' Tomitayou the idea was good, i had the same but once trying, i realized you must also remove specific key from self.inlines
because in change_view
and add_view
method self.get_inline_instances(request)
is called before get_formsets()
. Therefore i moved inlines handling to get_form()
method.
这是我成功完成的方式:
Here is how i sucessfully did it:
class SampleAdmin(ModelAdmin):
inlines = []
def get_inlines(self):
return [SampleInline, SampleInline2]
def get_form(self, request, obj=None, **kwargs):
# due to django admin form fields caching you must
# redefine inlines on every `get_form()` call
if (obj): self.inlines = self.get_inlines()
for inline in self.inlines:
# Here change condition based on your needs and manipulate
# self.inlines as you like (remove, change, etc).
# I used inline.__name__ to detect if this is correct inline
# for my obj
if obj.CONDITION:
self.inlines.remove(inline)
return super(SampleAdmin, self).get_form(request, obj, **kwargs)
这篇关于如何根据当前对象字段值显示不同的内联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!