Django Admin:动态添加内联 [英] Django Admin: add inlines dynamically
本文介绍了Django Admin:动态添加内联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
class MyTemplateAdmin(admin.ModelAdmin):
list_display = ('name')
search_fields = ['name']
inlines = [
Template1Inline,
Template2Inline,
Template3Inline,
]
这很好.但是我需要使它动态化.每当管理员将新模板添加到 MyTemplate 模型时,都需要将其添加到内联中.
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!
推荐答案
我尚未对此进行测试,但是从理论上讲,您可以这样做:
I haven't tested this, but in theory you could do:
class MyTemplateAdmin(admin.ModelAdmin):
def __init__(self, *args, **kwargs):
super(MyTemplateAdmin, self).__init__(*args, **kwargs)
#see if there are new templates
#and set the inlines list property
list_display = ('name')
search_fields = ['name']
希望对您有帮助.
这篇关于Django Admin:动态添加内联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文