Django admin, hide a model

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

    I had lots of model admins to register and hide, if you want a more DRY solution, this worked for me (Django 1.10, Python 3.5)

    # admin.py
    
    def register_hidden_models(*model_names):
        for m in model_names:
            ma = type(
                str(m)+'Admin',
                (admin.ModelAdmin,),
                {
                    'get_model_perms': lambda self, request: {}
                })
            admin.site.register(m, ma)
    
    register_hidden_models(MyModel1, MyModel2, MyModel3)
    

    I guess you could roll it into a utility class if you want to re-use it across apps.

提交回复
热议问题