4

I want to hide some models from admin index page and app page. For example, these that I have visible in inlines as related objects.
One more point, I want to keep the ability to work with change_view and add_view, but not list_view of that model.

Tried to find any hints in admin/templates/admin/*.html files, but haven't found anything helpful.

Is it possible without "hacks", monkey-patching and external libraries?

wowkin2
  • 5,895
  • 5
  • 23
  • 66

2 Answers2

3

You can try this

this will hide the model from the index

class YourModel(admin.ModelAdmin):
    def get_model_perms(self, request):    
        return {}  # return empty

admin.site.register(YourModel, YourModelAdmin)

more about Django admin https://docs.djangoproject.com/en/3.1/ref/contrib/admin/

rahul.m
  • 5,572
  • 3
  • 23
  • 50
  • Looks like what I need. Can you explain how it works and what else it can affect? For me, it looks that it can remove some permission which should be there. – wowkin2 Jan 25 '21 at 15:09
  • This removes all permissions, but to exclude only from index page - we can use `has_module_permission` - see answer below https://stackoverflow.com/a/65887364/3722635 – wowkin2 Jul 03 '23 at 08:01
3

As Django documentation tells:

ModelAdmin.has_module_permission(request)
Should return True if displaying the module on the admin index page and accessing the module’s index page is permitted, False otherwise. Uses User.has_module_perms() by default. Overriding it does not restrict access to the view, add, change, or delete views.

To avoid removal all permissions (like with get_model_perms()) you can redefine has_module_permission() method to always return False.

@admin.register(SomeModel)
class SomeModelAdmin(admin.ModelAdmin):

    def has_module_permission(self, request):
        return False
wowkin2
  • 5,895
  • 5
  • 23
  • 66