Subclassing Django’s ModelAdmin

After writing about sweetening URLFields on Django admin forms, I got to thinking, why not subclass ModelAdmin instead? It works!

from django.contrib import admin

class LocalModelAdmin(admin.ModelAdmin):
    class Media:
        js = (
            'js/tinymce/tiny_mce.js',
            'js/init/tinymce_advanced.js',
            'js/init/url_features.js',
            )

admin.py:

from path.to.module import LocalModelAdmin

class MyModelAdmin(LocalModelAdmin):
    ...

MyModel list and add/change forms now get the js files from LocalModelAdmin.

Advertisements