From 548fb65618dfce8aa43671f79231628a773a8f88 Mon Sep 17 00:00:00 2001 From: Matthew Tretter Date: Thu, 19 Jul 2012 20:01:40 -0400 Subject: [PATCH] Allow callables for AdminThumbnail image_field arg This allows images from related models to be displayed. Closes #138. --- imagekit/admin.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/imagekit/admin.py b/imagekit/admin.py index f764d3e..4466e6e 100644 --- a/imagekit/admin.py +++ b/imagekit/admin.py @@ -21,11 +21,14 @@ class AdminThumbnail(object): self.template = template def __call__(self, obj): - try: - thumbnail = getattr(obj, self.image_field) - except AttributeError: - raise Exception('The property %s is not defined on %s.' % \ - (self.image_field, obj.__class__.__name__)) + if callable(self.image_field): + thumbnail = self.image_field(obj) + else: + try: + thumbnail = getattr(obj, self.image_field) + except AttributeError: + raise Exception('The property %s is not defined on %s.' % \ + (self.image_field, obj.__class__.__name__)) original_image = getattr(thumbnail, 'source_file', None) or thumbnail template = self.template or 'imagekit/admin/thumbnail.html'