1

For Django's stock (out of the box) admin, adding custom JavaScript is as simple/easy as:

class ContentAdmin(admin.ModelAdmin):
    model = Content

    class Media:
        js = ('js/content.js',)

Works beautifully in Django 1.8.4 (have tested it).

Now I'm using django-xadmin in my project for a enhanced user interface/experience. The problem is that the code above doesn't add the custom JS to xadmin's views.

Went over the project's readme, "documentation" (or lack of) and even delved into the source code. The farthest I got was figuring out that overriding get_media() method it actually adds the custom JS to the view, but since it overrides the parent's call all other xadmin's JSs and CSSs aren't loaded.

class ContentAdmin(admin.ModelAdmin):
    model = Content

    class Media:
        js = ('js/content.js',)

    def get_media(self):
        # Tried "super(ContentAdmin, self).get_media()"
        ## » Says method doesn't exists
        # Tried "super(ContentAdmin, self).media"
        ## » Exactly the same thing as "self.media" below
        media = self.media
        print("#### MEDIA IS {}".format(media.__dict__))
        return media

That prints out:

#### MEDIA IS {'_css': {}, '_js': ['/static/admin/js/core.js', '/static/admin/js/admin/RelatedObjectLookups.js', '/static/admin/js/jquery.js', '/static/admin/js/jquery.init.js', '/static/admin/js/actions.js', 'js/content.js']}

My custom JS ('js/content.js') is definitely there and gets loaded, but all other default xadmin's CSS and JS are gone.

Any ideas on how to add the custom JS without overriding the parent's media properties? OR how to keep it when overriding?

2
  • It (xadmin) looks like a dead project... Commented Mar 3, 2016 at 12:50
  • It does have lots of open issues and PRs, but the owner is still around and actually commit a few changes every now and then. Commented Mar 3, 2016 at 12:51

1 Answer 1

4

It doesn't look like you are using django-xadmin's admin interface correctly.

With django-xadmin, your admin objects should not inherit from django's admin.ModelAdmin, just object. When you do xadmin.site.register(model,admin_class) in your app's adminx.py module, django-xadmin generates (and caches) the actual class dynamically based on plugins and the url pattern.

Your adminx.py should look something like:

import xadmin
from .models import Content

class ContentAdmin(object):

    def get_media(self):
        # media is the parent's return value (modified by any plugins)
        media = super(ContentAdmin,self).get_media()
        media.add_js(('js/content.js',))
        return media

 xadmin.site.regsiter(Content,ContentAdmin)
Sign up to request clarification or add additional context in comments.

2 Comments

Damn, tried something like that but guess I forgot to import xadmin. Just updated my code and tested it out - your solution works greatly, thanks a lot!
One minor change: it's def get_media(self):, the media parameter doesn't exists otherwise you get "TypeError: get_media() takes exactly 2 arguments (1 given)".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.