views:

597

answers:

2

I'm sort of stitching together contrib.databrowse (to view) and contrib.admin (to edit), and I'd like to override the *response_change* function in admin.ModelAdmin so that when you click save, it redirects back to the object in databrowse rather than the admin. I know how to do this for a specific model in admin.py, like:

class WhateverAdmin(admin.ModelAdmin):
    def response_change(self, request, obj):
        # stuff

admin.site.register(Whatever, WhateverAdmin)

but I'd like to apply that to all of my models without just copying the function over and over (i.e. override it in admin.ModelAdmin itself). Where and how should I do that?

Thanks. :)

+1  A: 

You could have your admin classes inherit off of the WhateverAdmin class.

So for example, your app is laid out as...

\site_defaults
    __init__.py
    admin.py
\your_app_1
    __init__.py
     models.py
     admin.py
\your_app_2
    __init__.py
     models.py
     admin.py

For the 2 apps you have, in the admin.py the code would be like...

from site_defaults.admin import WhateverAdmin

class YourApp1Admin(WhateverAdmin):  # << note it's not admin.ModelAdmin
    # code over here
T. Stone
Thanks, that was the kick in the head I needed..
WhoseyWhatsit
A: 

Here's my solution thanks to T. Stone's Idea.

I didn't think to register each model with the same Admin class if I wasn't changing anything else. The ones that need special changes inherit the new class, the rest call admin.site.register with it:

class MyModelAdmin(admin.ModelAdmin):
    def response_change(self, request, obj):
        # code 

class SomethingAdmin(MyModelAdmin): 
    # code specific to the Something model 

admin.site.register(Something, SomethingAdmin)
admin.site.register(Another, MyModelAdmin) 
admin.site.register(OneMore, MyModelAdmin)
WhoseyWhatsit