How to use custom AdminSite class?

前端 未结 3 903
南旧
南旧 2020-12-02 14:45

Which is the best way to implement my own django.contrib.admin.sites.AdminSite?

Actually I get a problem with the registration of INSTALLED_APPS

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-02 15:28

    From Django 2.1, there is an 'out-of-the-box' solution: https://docs.djangoproject.com/en/2.1/ref/contrib/admin/#overriding-the-default-admin-site

    from django.contrib import admin
    
    class MyAdminSite(admin.AdminSite):
    ...
    

    Swapping the custom admin site is now done by adding your own AdminConfig to installed apps.

    from django.contrib.admin.apps import AdminConfig
    
    class MyAdminConfig(AdminConfig):
        default_site = 'myproject.admin.MyAdminSite'
    


    INSTALLED_APPS = [
        ...
        'myproject.apps.MyAdminConfig',  # replaces 'django.contrib.admin'
        ...
    ]
    

    Note the difference between AdminConfig and SimpleAdminConfig, where the latter doesn't trigger admin.autodiscover(). I'm currently using this solution in a project.

提交回复
热议问题