Django admin model Inheritance is it possible?

徘徊边缘 提交于 2019-12-05 10:57:26

Maybe it is bit to late for you for the answer, but I think others can have similar problem - as I did.

Here is my solution - I am not sure if it is proper, but it works for me and non other from above can do the same (assuming that you want a multitable inheritance (non abstract model), as I do)

class SiteEntityAdmin(admin.ModelAdmin):
    fieldsets = [
            (None, {'fields': ['name']}),
    ]


class PhotoAdmin(SiteEntityAdmin):
    fieldsets = [
             ('Photo details', {'fields': ['photo_url', 'description']}),
    ]
    fieldsets.insert(0, SiteEntityAdmin.fieldsets[0])

Yes it's possible. I think the error you done is to put:

class Meta:
    abstract = True

in your Abstract_Admin_Model class. Try without the Meta class.

The problem is here:

class Admin_Topic( admin.ModelAdmin ):

This line controls the inheritance, so it should be:

class Admin_Topic( Abstract_Admin_Model ):

Also worth noting: you may wish to use TopicAdmin rather than Admin_Topic to better match the Django convention.

Try changing:

    class Meta:
        abstract = True

to

    class Meta:
        model = Topic
        abstract = True

The inheritance in your modified admin.py works. The problem is that you are adding the field 'created_at' to the admin (Admin_RSSFeed), but it does not exist on the model (probably named RSSFeed?). (At least that is what the error screenshot tries to tell you.)

To use the parent's class attributes, such as list_display or search_fields you can do the following:

@admin.register(BaseClass)
class BaseClassAdmin(admin.ModelAdmin):
    list_display = ('field_a', 'field_b')
    search_fields = ('field_a', 'field_b')

@admin.register(ChildClass)
class ChildClassAdmin(BaseClassAdmin):
    def get_list_display(self, request):
        return self.list_display + ('field_c', 'field_d')

    def get_search_fields(self, request):
        return self.search_fields + ('field_c', 'field_d')

Similarly you can do that for other attributes like actions, readonly_fields, etc.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!