如何根据当前对象字段值显示不同的内联
给定一个名为 MainModel
的模型和一个 RelatedModel
,其中后者具有 MainModel
的 ForeignKey
字段:
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 类:
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
,并显示 的内联相关模型2
时错误
。
我打算为 ModelAdmin.inline_instances
属性创建一个 描述符,但是我意识到我需要访问正在编辑的对象实例,但它作为参数传递。
有什么帮助吗?
谢谢!
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)
and the corresponding ModelAdmin classes:
class RelatedModel1InlineAdmin(admin.TabularInline):
model = RelatedModel1
class RelatedModel2InlineAdmin(admin.TabularInline):
model = RelatedModel2
class MainModel(admin.ModelAdmin):
inlines = [RelatedModel1, 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
.
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.
Any help?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
@Yuji 'Tomita' Tomitayou 这个想法很好,我也有同样的想法,但一旦尝试,我意识到你还必须从
self.inlines
中删除特定的键,因为在change_view
和 < code>add_view 方法self.get_inline_instances(request)
在get_formsets()
之前调用。因此,我将内联处理移至 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 inchange_view
andadd_view
methodself.get_inline_instances(request)
is called beforeget_formsets()
. Therefore i moved inlines handling toget_form()
method.Here is how i sucessfully did it:
我意识到这个问题有点老了,代码库也发生了一些变化;现在有一个干净的点可以覆盖一些东西:
get_inline_instances
。你可以这样做:I realize this question's a bit old and the codebase has changed a bit; there's a cleanish point to override things at now:
get_inline_instances
. You can do this:您只需在 ModelAdmin 中覆盖
change_view
即可:这对我来说很有效。
You need just simply override
change_view
in ModelAdmin:that's work for me.
在这篇旧文章中寻找同一问题的答案时,这对我有用。
扩展 darklow 的答案,我认为您可以简单地完全覆盖
get_inline_instances
并根据您的类型添加额外的检查。在模型中添加布尔类型检查方法
基于类型检查添加内联实例 - 只需将父类中的 get_inline_insances 方法复制并粘贴到 admin.ModelAdmin 类中,然后添加 if 块检查模型类型,如下所示
This worked for me while searching for an answer to the same problem in this old post.
Expanding upon darklow's answer , I think you can simply override
get_inline_instances
completely and add an extra check based on your type.Add a boolean type check method in your model
Add inline instance base on type check - Simply copy and paste the get_inline_insances method from the parent class into your admin.ModelAdmin class and add the if block to check the model type as shown below
从查看
contrib.admin.options.py
看来您可以覆盖ModelAdmin.get_formsets
。请注意,管理站点在__init__
处填充self.inline_instances
,因此您可能希望遵循而不是一遍又一遍地实例化您的内联。我不确定它有多贵:)原始的 admin
get_formsets
使用生成器 - 你也可以更接近地模仿原始的:From peeking at
contrib.admin.options.py
Looks like you could overrideModelAdmin.get_formsets
. Note that the admin site populatesself.inline_instances
at__init__
, so you probably want to follow and not instantiate your inlines over and over. I'm not sure how expensive it is : )The original admin
get_formsets
uses generators - you could too to more closely mimic the original:这是我遇到同样问题时编写的一段代码。我想,这有点暴力风格,但非常敏捷,应该适合所有情况。
Here is a piece of code I wrote when I was faced with the same problem. It is a bit brute force style, I guess, but is very agile and should suit all cases.