0

I am using django 1.4, python 2.7, Memcache, python-memcached, and easy-thumbnails.

When I try to access an item page using cached data I get the following template error:

Couldn't get the thumbnail uploads/items/item_images/logo.jpeg: 'ImageFieldFile' object has no attribute 'instance'

When I access the data in question from the data base in the shell I get:

>>> log = item.get_logo()
>>> logo
<ImageFieldFile: uploads/items/item_images/logo.png>
>>> logo.instance
<Media: uploads/items/item_images/logo.png>

When I try to access the same data from cache I get:

>>> cache.set('logo',item.get_logo())
>>> logo = cache.get('logo')
>>> logo
<ImageFieldFile: uploads/items/item_images/logo.png>
>>> logo.instance
Traceback (most recent call last):
  File "<console>", line 1, in <module>
AttributeError: 'ImageFieldFile' object has no attribute 'instance'

My question is how do you cache an ImageFieldFile so that it is retrievable in its original state? I need to pass that object to my template for use with easy-thumbnails.

Clayton
  • 285
  • 1
  • 17
  • Django serializes querysets and objects when you cache them because memcached is a key value store. My guess is it stops short of serializing images, maybe because it is a byte string? Can you cache the byte string yourself using the id or something as a key? – Enrico Nov 29 '12 at 02:10
  • @Enrico I looked into your suggestion and could not get it to work. My answer below is my current solution but am open to other suggestions. – Clayton Nov 29 '12 at 23:16

1 Answers1

2

So instead of trying to cache an ImageFieldFile I cached the object (Media) that ImageFieldFile resides in and now everything works.

Database:

>>> logo = item.get_logo()
>>> logo
<Media: uploads/items/item_images/logo.png>
>>> logo.image #this is what I was trying to cache before
<ImageFieldFile: uploads/items/item_images/logo.png>
>>> logo.image.instance
<Media: uploads/items/item_images/logo.png>

Cache:

>>> cache.set('logo',logo)
>>> cachedLogo = cache.get('logo')
>>> logo
<Media: uploads/items/item_images/logo.png>
>>> logo.image
<ImageFieldFile: uploads/items/item_images/logo.png>
>>> logo.image.instance
<Media: uploads/items/item_images/logo.png>
Clayton
  • 285
  • 1
  • 17