Kristiyan Kostadinov
Kristiyan Kostadinov

Reputation: 3712

Django admin exclude subclass

I have a couple of models that look along the lines of:

class Page(SomeBaseClass):
    # random fields


class Link(Page):
    # some other fields

And in my admin.py I've registered these classes:

admin.site.register(Page) # shows both Pages and Links
admin.site.register(Link) # shows only Links

At this point the "Pages" tab in the Admin shows both Page and Link and the "Links" tab shows only Link. Is it possible to exclude the Link model from the "Pages" tab?

Upvotes: 0

Views: 385

Answers (1)

Alasdair
Alasdair

Reputation: 309009

You could override get_queryset for the model admin, and use isnull to filter objects without a child.

class PageAdmin(admin.ModelAdmin):
    def get_queryset(self, request):
        qs = super(PageAdmin, self).get_queryset(request)
        return qs.filter(link__isnull=True)

admin.site.register(Page, PageAdmin)

Upvotes: 4

Related Questions