我有模型 InspectorCheck 和同一模型的两个 ModelAdmin 类 - 一个显示已删除字段等于 False 的记录,第二个显示已删除记录。
此函数创建从主模型继承的新代理模型类:
def create_proxymodel(model_from, name=None, attrs=None, meta=None):
attrs, meta = (attrs if attrs else {}, meta if meta else {})
class Meta:
proxy = True
app_label = model_from._meta.app_label
for k, v in meta.items():
setattr(Meta, k, v)
attrs.update({'__module__': '', 'Meta': Meta})
newmodel = type(name, (model_from,), attrs)
return newmodel
两个用于不同目的的 ModelAdmins:
class InspectorCheckAdmin(admin.ModelAdmin):
list_display = ('id', 'check_address', 'check_form', 'check_target_name')
class InspectorCheckAdminDeleted(admin.ModelAdmin): # or inherit from other ModelAdmin
list_display = ('id', 'check_address', 'check_form', 'check_target_name', 'editor', 'updated')
def queryset(self, request):
return InspectorCheck.trash
#and in last step create and register both classes:
InspectorCheckDeleted = create_proxymodel(InspectorCheck, 'InspectorCheckDeleted', meta={
'verbose_name': u'Inspector check (deleted)',
'verbose_name_plural': u'Inspector checks (deleted)'}
)
admin.site.register(InspectorCheck, InspectorCheckAdmin)
admin.site.register(InspectorCheckDeleted, InspectorCheckAdminDeleted)