class MyTemplateAdmin(admin.ModelAdmin):
list_display = ('name')
search_fields = ['name']
inlines = [
Template1Inline,
Template2Inline,
Template3Inline,
]
这很好用。但我需要的是让它充满活力。每当管理员向 MyTemplate 模型添加新模板时,都需要将其添加到内联中。
有办法做到这一点吗?如果我对我的问题不够清楚,请发表评论。 提前谢谢!
答案 0 :(得分:0)
我没有测试过这个,但理论上你可以这样做:
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']
希望能帮到你。
答案 1 :(得分:0)
在模板的admin.py中:
class Template1Inline(admin.TabularInline)
pass
class Template2Inline(admin.TabularInline)
pass
然后在admin.py中为MyTemplateAdmin:
import sys, inspect, Templates.admin
class MyTemplateAdmin(admin.ModelAdmin):
list_display = ('name')
search_fields = ['name']
def __init__(self, *args, **kwargs):
inlines = [class_type[1] for class_type in inspect.getmembers(Templates.admin, inspect.isclass)]
super(MyTemplateAdmin, self).__init__(*args, **kwargs)
Templates.admin
可能不正确,具体取决于您的项目设置方式,但关键是您只需导入具有Template1Inline
类的模块。
答案 2 :(得分:0)
只是一个简单的想法。
from django.contrib import admin
from mymodule import signals
class MyModuleAdmin(admin.ModelAdmin):
def add_view(self, *args, **kwargs):
signals.my_dynamic_inline_hook_signal.send(
sender = self,
inlines = self.inlines,
args = args,
kwargs = kwargs
)
答案 3 :(得分:0)
我不完全确定这是你在找什么。您想要内联是同一模型的不同实例吗? 动态创建内联的一种方法是使用type()并将它们添加到get_inline_instances()
中-(RequestResult*)makeSyncJsonRequest{
__block RequestResult *retResult = [[RequestResult alloc] init];
__block BOOL block = YES;
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_group_t group = dispatch_group_create();
void (^resultBlock)(RequestResult*) = ^(RequestResult* result){
if(!retResult.error)
retResult = result;
block = NO;
dispatch_group_leave(group);
};
// Add a task to the group
dispatch_group_async(group, queue, ^{
// Some asynchronous work
dispatch_group_enter(group);
[self makeAsyncJsonRequestWithBlock:resultBlock];
});
// Do some other work while the tasks execute.
// When you cannot make any more forward progress,
// wait on the group to block the current thread.
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
return retResult;
}
答案 4 :(得分:0)
只需覆盖管理员的get_inline_instances.
def get_inline_instances(self, request, obj=None):
_inlines = super().get_inline_instances(request, obj=None)
custom_inline = YourDynamicInline(self.model, self.admin_site)
_inlines.append(custom_inline)
return _inlines