开发者

Can you register multiple ModelAdmins for a Model? Alternatives?

开发者 https://www.devze.com 2023-02-05 17:06 出处:网络
Say I have the Dja开发者_运维百科ngo model class: class Foo(models.Model): bar = models.CharField()

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) 
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号