Adding to @dominic, I wanted to use this in several models, so I created a function that I could call in each model by entering an image to be displayed.
For example, in one application, I have:
from django.contrib import admin from .models import Frontpage from ..admin import image_file class FrontpageAdmin(admin.ModelAdmin): list_display = ('image_thumb', ...) image_thumb = image_file('obj.image()') admin.site.register(Frontpage, FrontpageAdmin)
with image function Frontpage, which returns an image.
In another application, I have:
from django.contrib import admin from .models import Exhibition from ..admin import image_file class ExhibitionAdmin(admin.ModelAdmin): list_display = ('first_image_thumb', ...) first_image_thumb = image_file('obj.related_object.image', short_description='First Image') admin.site.register(Exhibition, ExhibitionAdmin)
This allows me to specify the image object and short_description , saving the template in another file. Function:
from sorl.thumbnail import get_thumbnail from django.conf import settings def image_file(image, short_description='Image'): def image_thumb(self, obj): image = eval(image_thumb.image) if image: thumb = get_thumbnail(image.file, settings.ADMIN_THUMBS_SIZE) return u'<img width="{}" height={} src="{}" />'.format(thumb.width, thumb.height, thumb.url) else: return "No Image" image_thumb.__dict__.update({'short_description': short_description, 'allow_tags': True, 'image': image}) return image_thumb
saul.shanabrook Jul 13 2018-12-18T00: 00Z
source share