我正在使用django 1.4,python 2.7,Memcache,python-memcached和easy-thumbnails。
当我尝试使用缓存数据访问项目页面时,出现以下模板错误:
Couldn't get the thumbnail uploads/items/item_images/logo.jpeg: 'ImageFieldFile' object has no attribute 'instance'
当我从 shell 中的数据库访问有问题的数据时,我得到:
>>> log = item.get_logo()
>>> logo
<ImageFieldFile: uploads/items/item_images/logo.png>
>>> logo.instance
<Media: uploads/items/item_images/logo.png>
当我尝试从缓存中访问相同的数据时,我得到:
>>> 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'
我的问题是你如何缓存ImageFieldFile
以便它可以在原始状态下检索?我需要将该对象传递给我的模板以与简易缩略图一起使用。
因此,
我没有尝试缓存ImageFieldFile
而是缓存了ImageFieldFile
所在的对象(Media
),现在一切正常。
数据库:
>>> 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.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>