Django Cache - 显示从缓存中检索的图像的问题

时间:2012-11-29 01:53:53

标签: django python-memcached

我使用的是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以便它可以在原始状态下检索?我需要将该对象传递给我的模板,以便与简单缩略图一起使用。

1 个答案:

答案 0 :(得分:2)

因此,我没有尝试缓存ImageFieldFile缓存Media所在的对象(ImageFieldFile),而是现在一切正常。

<强>数据库:

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