首页 > 解决方案 > Why am I getting this PicklingError?

问题描述

I'm working on a Python/Django/Wagtail project, and I have some api to return some paginated articles. It goes like this:

In URLs:

url(r'^morearticles/', views.get_live_articles),

In Views:

def get_live_articles(request):
  context = {'articles': getLiveArticles(request) }
  return render(request, 'app/components/articles-live.html', context, content_type="text/html; charset=utf-8")

The getLiveArticles function looks like this:

def getLiveArticles(request):

 # Articles
 a = get_articles() #this is getting the articles correctly
 p = Paginator(a, 4)
 page_n = request.GET.get('page')

 try:
     articles = p.page(page_n)
 except Exception, e:
     articles = []

 return articles

However when hitting the api endpoint I get this:

    Traceback (most recent call last):
  File "/Users/john/.virtualenvs/upgrade/lib/python2.7/site-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/Users/john/.virtualenvs/upgrade/lib/python2.7/site-packages/django/core/handlers/base.py", line 249, in _legacy_get_response
    response = self._get_response(request)
  File "/Users/john/.virtualenvs/upgrade/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Users/john/.virtualenvs/upgrade/lib/python2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/john/app/app/views.py", line 90, in get_live_articles
    context = {'articles': getLiveArticles(request) }
  File "/Users/john/.virtualenvs/upgrade/lib/python2.7/site-packages/cache_utils/decorators.py", line 48, in wrapper
    cache.set(key, value, timeout, **backend_kwargs)
  File "/Users/john/.virtualenvs/upgrade/lib/python2.7/site-packages/django/core/cache/backends/locmem.py", line 75, in set
    pickled = pickle.dumps(value, pickle.HIGHEST_PROTOCOL)
PicklingError: Can't pickle <class 'wagtail.wagtailcore.blocks.base.RichTextBlockMeta'>: attribute lookup wagtail.wagtailcore.blocks.base.RichTextBlockMeta failed

I've came upon the Pickling error before, but never quite understood what it is about. Any idea of what could be causing this?

Let me know if I should provide more information. After debugging I think the issue has to be contained in these chunks of code, but I might be wrong.

EDIT: One of the fields of the objects has been turned into a StreamField object containing the main content lately. May that have something to do?

标签: pythondjangopicklewagtail

解决方案


我认为这是与缓存相关的(最近有同样的问题),其中一些对象不能被腌制以进行缓存,并且您已经确认您正在使用缓存。所以禁用缓存可以解决问题。

我建议使用 pickle 兼容的对象类型。另一方面,在Python 文档中,有一些指导方针如何实现/自定义酸洗/解酸逻辑。


推荐阅读