I have been trying to minimize queries related to image thumbnails and you can do something like this:
- prefetch all image renditions
- use lru cache to 'cache' the most used images renditions
I have not completed my optimizations and code cleanup, but it is like I was playing around.
@lru_cache(maxsize=200)
def get_image_renditions(image):
return image.renditions.all().select_related('filter')
def get_rendition(image, filter_spec):
renditions = get_image_renditions(image)
for r in renditions:
if r.filter.spec == filter_spec:
return r
return image.get_rendition(filter_spec)
@register.assignment_tag
def image_rendition(image, filter_spec):
return get_rendition(image, filter_spec)
# using a templatetag as "wrapper"
{% image_rendition post.author.profile_picture fill-270x270 as avatar_image %}