假设我有Django模型类:
class Foo(models.Model):
bar = models.CharField()
baz = models.CharField()
和ModelAdmins:
class Foo_Admin_1(admin.ModelAdmin):
list_display = ['id','bar']
class Foo_Admin_2(admin.ModelAdmin):
list_display = ['id','baz']
有没有办法注册两个ModelAdmins,以便它们显示在Django Admin界面下?
我试过了:
admin.site.register(Foo,Foo_Admin_1)
admin.site.register(Foo,Foo_Admin_2)
但是我收到了错误:
The model Foo is already registered
有什么建议吗?
如果没有,是否有其他方法(动态)控制ModelAdmin更改列表视图中显示的字段?
答案 0 :(得分:11)
创建一个空的代理子类并改为注册:
class Foo(models.Model):
bar = models.CharField()
baz = models.CharField()
# admin.py
class FooProxy(Foo):
class Meta:
proxy=True
admin.site.register(Foo, FooAdmin1)
admin.site.register(FooProxy, FooAdmin2)