Allow callables for AdminThumbnail image_field arg

This allows images from related models to be displayed. Closes #138.
This commit is contained in:
Matthew Tretter 2012-07-19 20:01:40 -04:00
parent 70ab4a0cc0
commit 548fb65618

View file

@ -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'