Overriding third party admin

Last modified Feb. 25, 2009 | Revision 6

(mattw doesn’t have his own development blog, so he’s putting this here…)

With the advent of newforms-admin it’s now possible to override admin interfaces without having to change any code in third-party modules. This example shows how to enable a rich-text editor for django.contrib.flatpages without touching Django’s code at all.

First, I created admin.py in my project root:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
    from django.contrib import admin
    from django.contrib.flatpages.models import FlatPage
    
    # Override flatpage admin
    class FlatPageAdmin(admin.ModelAdmin):
        class Media:
            js = ('/media/j/jquery.js', 
                  '/media/j/admin_enhancements.js')
            css = {'screen': ('/media/c/admin.css',)}
        
    admin.site.unregister(FlatPage)
    admin.site.register(FlatPage, FlatPageAdmin)

Because this is not in an app directory, it won’t get automatically imported. So we wire it up in our main urls.py. Where my urls.py currently looks like:

from django.contrib import admin
admin.autodiscover()

I’m going to change it to this:

from django.contrib import admin
admin.autodiscover()
import myproject.admin

This is enough to activate our new custom admin interface.

Last modified Feb. 25, 2009 | Revision 6