Django admin, hide a model

后端 未结 8 972
广开言路
广开言路 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:14

    This is an alternative building on top x0nix's answer, and only if you are happy hiding the the rows with jquery.

    Copy pasting from the other answer the part that I reused

    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
    
    class MyModelAdmin(HiddenModelAdmin):
    ...
    
    admin.site.register(MyModel, MyModelAdmin)
    

    Then install django-jquery and then add the following block in your /admin/index.html template:

    {% extends "admin:admin/index.html" %}
    
    {% block extrahead %}
        
        {% if app_list %}
          
       {% endif %}
    {% endblock %}
    

    You don't need to copy paste the whole template, just extend it and override the extrahead block. You'll need django-apptemplates for the above to work.

提交回复
热议问题