Django Admin:动态添加内联
class MyTemplateAdmin(admin.ModelAdmin):
list_display = ('name')
search_fields = ['name']
inlines = [
Template1Inline,
Template2Inline,
Template3Inline,
]
这很好用。但我需要的是让它充满活力。每当管理员将新模板添加到 MyTemplate 模型时,都需要将其添加到内联中。
有办法做到这一点吗?如果我对我的问题不够清楚,请发表评论。 提前致谢!
class MyTemplateAdmin(admin.ModelAdmin):
list_display = ('name')
search_fields = ['name']
inlines = [
Template1Inline,
Template2Inline,
Template3Inline,
]
This works fine. But what I need is to make it dynamic. Whenever the admin adds a new Template to the MyTemplate Model, that needs to be added to the inlines.
Is there a way to do this? Please comment if I am not clear enough on my question.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
只需覆盖管理员的 get_inline_instances 即可。
Just Override the admin's get_inline_instances.
我还没有测试过这个,但理论上你可以这样做:
希望对你有帮助。
I haven't tested this, but in theory you could do:
Hope that helps you out.
在模板的 admin.py 中:
然后在 MyTemplateAdmin 的 admin.py 中:
Templates.admin
可能不正确,具体取决于您的项目设置方式,但重点是您只需导入以下模块:有Template1Inline
类。In admin.py for the Templates:
Then in the admin.py for MyTemplateAdmin:
Templates.admin
may not be correct depending on how you have your project setup, but the point is you just import the module that has theTemplate1Inline
classes.只是一个快速的想法。
Just a quick idea.
我不完全确定这就是您正在寻找的东西。您想要内联是同一模型的不同实例吗?
动态创建内联的一种方法是使用 type() 并将它们添加到 get_inline_instances() 中
I'm not completely sure this is what you are looking for. You want inlines that are different instances of the same model?
One way of creating the inlines dynamically is with type() and adding them in get_inline_instances()