Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Admin exclude model from index and app page

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?

like image 544
wowkin2 Avatar asked Oct 13 '25 00:10

wowkin2


2 Answers

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
like image 170
wowkin2 Avatar answered Oct 14 '25 13:10

wowkin2


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/

like image 34
rahul.m Avatar answered Oct 14 '25 13:10

rahul.m