Django grappelli adding custom js to change_form

1.3k views Asked by At

I try to add my custom javascript to some change_form templates. Before having installed Grappelli, I could do that by extending change_form.html of admin and creating sub folders under the templates with my module names.

For example my tree hireachy was like :

+templates 
++admin_copies 
+++change_form.html (I have added extra js blocks) 
++admin 
+++employer 
++++employer 
+++++change_form.html 
++++employer_new 
+++++change_form.html

As you understand I have a model named "Employer". I can add my custom js methods under the change_form.html files. However, this kind of inheritance cause javascript/breadcrumbs problems with Grappelli. How can I add simply my custom javascript functions for each model separetly ?

1

There are 1 answers

0
Simon Kagwi On BEST ANSWER

Create a subclass of ModelAdmin, and define the custom js in its Media class.

admin.py, in the same folder as the models.py that has Employer:

from django.contrib import admin
from models import Employer

class EmployerAdmin(admin.ModelAdmin):
    class Media:
        js = ("js/custom.js",) # This paths are appended to your MEDIA_URL setting

admin.site.register(Employer, EmployerAdmin)

Read more here