Categories
Python Answers

How to add Multiple ModelAdmins/views for same model in Python Django admin?

Spread the love

To add Multiple ModelAdmins/views for same model in Python Django admin, we can call register with different ModelAdmin and model classes.

For instance, we write

class PostAdmin(admin.ModelAdmin):
    list_display = ('title', 'pubdate','user')

class MyPost(Post):
    class Meta:
        proxy = True

class MyPostAdmin(PostAdmin):
    def get_queryset(self, request):
        return self.model.objects.filter(user = request.user)


admin.site.register(Post, PostAdmin)
admin.site.register(MyPost, MyPostAdmin)

to call admin.site.register with the Post model and PostAdmin model admin classes.

And we call admin.site.register with the MyPost model and MyPostAdmin model admin classes.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *