简体   繁体   中英

override AdminSite make admin.py another app doesn't show at django admin

On my project, there are multiple app, so i also have admin.py each folder. Here are some of my admin.py

api/admin.py

from django.contrib import admin
from .models import Message

@admin.register(Message)
class MessageAdmin(admin.ModelAdmin):
    list_display = ['source','name','created']
    list_filter = ['source','created']
    search_fields = ['message']

dummy/admin.py

from django.contrib.admin import AdminSite
from dummy.forms import MyAdminLoginForm

# export admin.py another app
from api.models import Message
from api.admin import MessageAdmin

class MyAdminSite(AdminSite):
    login_form = MyAdminLoginForm
    login_template = 'dummy/templates/admin/login.html'

site = MyAdminSite()
site.register(Message,MessageAdmin)

Since i wanna use captcha on django admin login, i should override the login form that inherit from AdminSite via dummy/admin.py. After that i registering the custom login on the main url

from django.contrib import admin
from django.urls import path, include
from dummy.admin import site
admin.site = site

urlpatterns = [
    path('admin/', admin.site.urls),
    path('api/', include('api.urls'))
]

You can see that i re registering the api/admin.py on the dummy/admin.py, i did it because if i didn't re registering the api/admin.py, it won't be show at the django admin. what should i do to make the admin.py another app doesn't need to be re register like it used to be when i use custom login form that inherit from AdminSite

You could just patch and use the original admin.site :

from django.contrib import admin

admin.site.login_form = MyAdminLoginForm
admin.site.login_template = 'dummy/templates/admin/login.html'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM