'Django admin - custom admin page for specific users

In my main url router I have two separate urls, one for each admin page that I set up in my admin.py file:

  • /adminmain/ URL goes to the default admin panel (admin.site.urls) with many different models registered.

  • /adminonly/ URL goes to a custom admin site (PostsAdmin2) and I want this site to work for users where is_admin = True in their user profile.

So far my admin.py file below isn't achieving this.

If I log in as a superuser then both urls will show the models registered to the respective admin page (this is fine).

But if I login as a non-superuser who IS an admin (is_admin = True) I just get a message: You don't have permission to view or edit anything. on BOTH pages. I want that user to see the models registered for the /adminonly/ page since I gave permission to do so in the PostsAdmin2 admin site.

My admin.py:

from django.contrib.admin import ModelAdmin, site, AdminSite
from .models import Post

class PostAdmin(ModelAdmin):

    list_display = (
        'id',
        'slug',
        'title',
        'author',
        'publish_date'
    )
   
    def has_view_permission(self, request, obj=None):
        if request.user.is_admin:
            return True

    def has_add_permission(self, request):
        if request.user.is_admin:
            return True

    def has_change_permission(self, request, obj=None):
        if request.user.is_admin:
            return True

    def has_delete_permission(self, request, obj=None):
        if request.user.is_admin:
            return True

class PostsAdmin2(AdminSite):
    site_header = 'Posts Admin'

    def has_permission(self, request):
        return request.user.is_admin

posts_site2 = PostsAdmin2(name='PostsAdmin')

#default admin page- only for superusers, /adminmain/ URL goes here
site.register(Post, PostAdmin)
site.register(..many more models and Admin classes registered)

#second admin page- meant for any user with is_admin = True,  /adminonly/ URL goes here
posts_site2.register(Post, PostAdmin2)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source