Setting an object in the Django cache API fails due to pickle error

回眸只為那壹抹淺笑 提交于 2019-12-24 00:34:01

问题


I'm trying to manually set an object in the Django cache API but it fails (i think due to pickling?) The object is given to me by a third party, my code is:

def index(request, template_name="mytemplate.htm"):
    user_list = cache.get("user_list_ds")
    if user_list is None:
            # this is the expensive bit I'm trying to cache
            # given to me by a third part
        user_list = graffiti.user_list("top", 100).responseObj().blocks()
        cache.set("user_list_ds", user_list, 10*60) # 10 minutes

    return render_to_response(template_name, { 'user_list' : user_list,}, context_instance = RequestContext(request))

When I run this I get an error;

Can't pickle <type 'etree._Element'>: import of module etree failed
in -    cache.set("user_list_ds", user_list, 10*60) # 10 minutes 

I'm very new to python, and I'm wondering how best to resolve this, do i need to pickle something first?


回答1:


It appears that you need to install ElementTree, because the pickle operation tries and fails to import the etree module.

UPDATE: Looking at it further, are you actually trying to cache document nodes? If you're trying to cache the data from the node, you probably need to do some processing of the value you're currently storing in user_list.



来源:https://stackoverflow.com/questions/1615721/setting-an-object-in-the-django-cache-api-fails-due-to-pickle-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!