Can you register multiple ModelAdmins for a Model? Alternatives?
Say I have the Dja开发者_运维百科ngo model class:
class Foo(models.Model):
bar = models.CharField()
baz = models.CharField()
and the ModelAdmins:
class Foo_Admin_1(admin.ModelAdmin):
list_display = ['id','bar']
class Foo_Admin_2(admin.ModelAdmin):
list_display = ['id','baz']
is there any way to register both ModelAdmins so that they show up under the Django Admin interface?
I tried:
admin.site.register(Foo,Foo_Admin_1)
admin.site.register(Foo,Foo_Admin_2)
but I get the error:
The model Foo is already registered
Any suggestions?
If not, are there alternative ways to (dynamically) control the fields shown in the ModelAdmin change list view?
Create an empty proxy subclass and register it instead:
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)
精彩评论