schneck
schneck

Reputation: 10847

Add snippet in ModelAdmin

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 there any way to do this?

Upvotes: 2

Views: 415

Answers (2)

fish2000
fish2000

Reputation: 4435

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.

Upvotes: 1

Mark Lavin
Mark Lavin

Reputation: 25164

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.

Upvotes: 2

Related Questions