Django admin, hide a model

后端 未结 8 1015
广开言路
广开言路 2020-11-30 23:42

At the root page of the admin site where registered models appear, I want to hide several models that are registered to the Django admin.

If I directly unregister th

8条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-01 00:17

    As of Django 1.8.18, has_module_permission() still has issue. So, in our case we used also the get_model_perms(). Likewise, we need to hide the model for specific user only, but the superuser should be able to access its index entry.

    class MyModelAdmin(admin.ModelAdmin):
        def get_model_perms(self, request):
            if not request.user.is_superuser:
                return {}
            return super(MyModelAdmin, self).get_model_perms(request)
    
    admin.site.register(MyModel, MyModelAdmin)
    

提交回复
热议问题