I have a ModelAdmin where I need to insert some html-snippet that is not part of a model (it's a java-applet). Is th开发者_开发技巧ere any way to do this?
You have a couple options. If the applet is related to one of the form fields then you could create a custom widget which includes the applet. Another way would be to override the template used by the model change form and include the applet. The template should be in admin/app_name/model_name/change_form.html
in your templates directory where app_name
and model_name
are replaced by the appropriate values for your model.
I tend to do a lot of this sort of thing, which is pretty much what you seem to want:
class SomeModelAdmin(admin.ModelAdmin):
...
list_display = (
'visible',
'thumbnail',
'size',
'url',
)
...
def thumbnail(self, obj):
return u'<img src="%s" />' % obj.url
thumbnail.allow_tags = True
... et voila, ad-hoc HTML snippets. obj
is the model instance in question. Personally I find this more flexible than endlessly subclassing Widgets, ModelForms et al -- your mileage may vary depending on what you do with the admin site, or if your're of the more orthodox object-oriented persuasion; it's helpful to know how to do it in any case.
精彩评论