So che questa è una risposta piuttosto tardi, ma ho pensato che un'implementazione più completa sarebbe utile per gli altri ...
Se non lo avete già con django-Filer, ottenere easy_thumbnails pip install easy-thumbnails
.
# -*- coding: utf-8 -*-
from django.contrib import admin
from easy_thumbnails.files import get_thumbnailer
from models import Photo
class PhotoAdmin(admin.ModelAdmin):
list_display = ('_thumbnail', 'title',)
list_display_links = ('_thumbnail', 'title',) # This makes the icon clickable too
readonly_fields = ('_thumbnail',)
fields = ('title', 'photo',)
def _thumbnail(self, obj):
if obj.photo:
thumbnailer = get_thumbnailer(obj.photo)
thumb = thumbnailer.get_thumbnail({
'crop': True,
'size': (50, 50),
# Sharpen it up a little, since its so small...
'detail': True,
# Put other options here...
})
# Note: we get the actual width/height rather than
# hard-coding 50, 50, just to be DRYer
return u'<img src="%s" alt="thumbnail: %s" width="%d" height="%d"/>' % (thumb.url, obj.photo.name, thumb.width, thumb.height)
else:
return "[No Image]"
# Optional, Provide a nicer label in the display
_thumbnail.short_description = 'Thumbnail'
# Required, leaves the markup un-escaped
_thumbnail.allow_tags = True
admin.site.register(Photo, PhotoAdmin)
Grazie per i chiarimenti, non sono riuscito a trovarlo nei documenti. – Andy
@Andy, se questo funziona per te, fai clic sul segno di spunta sotto il punteggio questa risposta deve accettarlo. –
Non sono sicuro, ma penso che la sintassi corretta sia con '{}', non '% s'. Quindi sarebbe: 'format_html (u '', obj.admin_thumbnail.url)'. – Paolo