Django (1.10) override AdminSite

前端 未结 3 1797
你的背包
你的背包 2021-01-02 04:25

I\'ve tried to override AdminSite class with my own custom class. I followed tutorial from django\'s documentation: https://docs.djangoproject.com/en/1.10/ref/contrib/admin

3条回答
  •  攒了一身酷
    2021-01-02 04:50

    I faced a similar problem. I used Django 2.1 and the hook from the comments above didn't work for me. And also I was not able to import GroupAdmin and UserAdmin, like so

    from django.contrib.auth.models import Group, User
    from django.contrib.auth.admin import GroupAdmin, UserAdmin
    

    Importing GroupAdmin or UserAdmin broke the code for some reason. I was not able to define the exact reason.

    So my workaround was (in project/urls.py):

    from django.conf.urls import include, url
    from django.contrib.admin import site
    from project.admin import myadmin
    
    myadmin._registry.update(site._registry)
    
    urlpatterns = [
        url(r'^admin/', myadmin.urls),
        ]
    

    The idea here is to copy registered models from default admin site. Maybe it's not good to do so, but I could not find anything else working.

提交回复
热议问题