Django admin, hide a model

后端 未结 8 1009
广开言路
广开言路 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条回答
  •  悲&欢浪女
    2020-12-01 00:26

    Got the same problem, here what I came up with.

    Like in previous solution - copy index.html from django to your /admin/index.html and modify it like this:

    {% for model in app.models %}
        {% if not model.perms.list_hide %}
        
        ...
        
        {% endif %}
    {% endfor %}
    

    And create ModelAdmin subclass:

    class HiddenModelAdmin(admin.ModelAdmin):
        def get_model_perms(self, *args, **kwargs):
            perms = admin.ModelAdmin.get_model_perms(self, *args, **kwargs)
            perms['list_hide'] = True
            return perms
    

    Now any model registered with HiddenModelAdmin subclass won't show up in admin list, but will be available via "plus" symbol in detail:

    class MyModelAdmin(HiddenModelAdmin):
        ...
    
    admin.site.register(MyModel, MyModelAdmin)
    

提交回复
热议问题