Differentiate ModelAdmin media for add/change pages in Django
To encapsulate customized functionalities in Django admin I'm providing a custom ModelAdmin
class that actually looks like this:
class ArticleAdmin(admin.ModelAdmin):
class Media:
js = ("my_code.js",)
I noted that my_code.js
gets loaded both in add and change pages, but I need to load it only in the add pa开发者_开发百科ge. Is it possible to do this (and how)?
Not sure how StackOverflow likes digging up old posts, but I found this doing research for my own problem and found a slightly more elegant solution than the accepted answer. You can over-ride therender_change_form
method to update the context['media']
definition. Sample for this question:
def render_change_form(self, request, context, add=False, change=False, form_url='', obj=None):
if add:
if context.has_key('media'):
old_media = context['media']
else:
old_media = forms.Media()
context.update({'media': old_media+forms.Media(js=("js/SPD_media_admin.js",)) })
return super(ArticleAdmin,self).render_change_form(request=request, context=context, add=add, change=change, form_url=form_url, obj=obj)
This works with Django 1.4. Interested readers may need to adjust the args
and kwargs
to match the method specifications. context
is not usually passed to this method as a kwarg
otherwise I think it could have been edited without explicitly listing the args
. Hope this is helpful to the OP or posterity.
Unfortunately the list of JavaScript files to include is generated and assigned by the media
context variable provided from the admin application's view function, which you can't easily override. But here's one not-so-elegant solution:
View source on the admin add page for your current model and copy the script tags that import the JavaScript files starting at line 14 (I'm using Django 1.2). You'll see where to paste them in the code fragment below.
Create your own custom change_form.html
template in your templates/admin/my_app/article
directory, and place the following contents in the file:
{% extends "admin/change_form.html" %}
{% block extrahead %}
{% url admin:jsi18n as jsi18nurl %}
<script type="text/javascript" src="{{ jsi18nurl|default:"../../../jsi18n/" }}"></script>
{% if add %}
{{ media }}
{% else %}
<!-- originally generated script tags: may differ depending on your model's fields -->
<script type="text/javascript" src="/media/admin/js/core.js"></script>
<script type="text/javascript" src="/media/admin/js/admin/RelatedObjectLookups.js"></script>
<script type="text/javascript" src="/media/admin/js/jquery.min.js"></script>
<script type="text/javascript" src="/media/admin/js/jquery.init.js"></script>
<script type="text/javascript" src="/media/admin/js/actions.min.js"></script>
<!-- end originally generated script tags -->
{% endif %}
{% endblock %}
Note that if you add a date field, for example, to your model, the field won't work properly on the change form when you edit a record, because a needed calendar.js
file won't be called in. You'd have to add it manually if you made such a change.
One alternative to go further with this and restore the lost dynamicness would be to create a template filter that you could pass the media
variable into and use a regex, for example, to strip out the unwanted script tag.
精彩评论