djangopython-memcached

Django Cache - Issue displaying images retrieved from cache


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.


Solution

  • 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>