开发者

Add snippet in ModelAdmin

开发者 https://www.devze.com 2022-12-24 01:21 出处:网络
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 r

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号