otto-torino / django-baton

A cool, modern and responsive django admin application based on bootstrap 5 that brings AI to the Django admin - https://baton.sqrt64.it/
MIT License
876 stars 95 forks source link

How to override an BattonAdminSite method #263

Closed pythoniste closed 1 year ago

pythoniste commented 1 year ago

Hello,

I need to overrides index method to do a redirection if some conditions are met, or let the page display otherwise.

I cannot use the usual way, obviously. But i think that overriding the BatonAdminSite class would work. It doesn't. I tried to duplicate the code of baton.autodiscover (and replace it in INSTALLED_APPS), and override the method index there, but it wont work.

So, I need some help : How can I override the index method of the admin site ?

abidibo commented 1 year ago

Hi @pythoniste you can do something like that:

  1. in your main app (I'll call it app_name) create the autodiscover/admin.py file with the following content (also include an __init__.py in the autodiscover folder):
from django.http import HttpResponse
from django.contrib import admin
from baton.autodiscover.admin import BatonAdminSite

class MyAdminSite(BatonAdminSite):
    def index(self, request, extra_context=None):
        return HttpResponse("I'm working!")

site = MyAdminSite()

# override otherwise in admindocs the default admin site is used showing
# the navbar
admin.site = site
  1. In your settings INSTALLED_APP replace baton.autodiscover with your app_name.autodiscover (as the last listed app)
  2. In your main urls.py replace the baton autodiscover import:
# from baton.autodiscover import admin # comment out this line
from app_name.autodiscover import admin # add this line

And everything should work

abidibo commented 1 year ago

Closing this, feel free to re-open if needed.