How can I override the "media" property of Django's ModelAdmin and make it dynamic?
Shortly after writing up the above question, I found a technique that works. Instead of defining a Media
class, I just manually add ALL of my css/js via the media
method:
from django.contrib import admin
class MyModelAdmin(admin.ModelAdmin):
model = MyModel
...
@property
def media(self):
media = super(MyModelAdmin, self).media
css = {
"all": (
"my/css/file1.css",
"my/css/file2.css",
)
}
js = [
"my/js/file1.js",
"my/js/file2.js",
]
if whatever_condition_I_want:
js.append("my/js/file3.js")
media.add_css(css)
media.add_js(js)
return media