Say I have the Django 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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With