为什么我会收到这个PicklingError?

时间:2018-06-05 18:01:50

标签: python django pickle wagtail

我正在开发Python / Django / Wagtail项目,我有一些api可以返回一些分页文章。它是这样的:

在网址中:

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

在视图中:

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

getLiveArticles 功能如下所示:

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

然而,当点击api端点时,我得到了这个:

    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

我之前发现过Pickling错误,但从未完全理解它是什么。有什么可能导致这种情况的想法吗?

如果我应该提供更多信息,请告诉我。调试后我认为问题必须包含在这些代码块中,但我可能错了。

编辑:对象的一个​​字段最近变成了一个包含主要内容的StreamField对象。愿有事可做吗?

1 个答案:

答案 0 :(得分:1)

我认为这是与缓存相关的(最近有同样的问题),其中某些对象无法进行缓存,并且您已经确认使用了缓存。因此,禁用缓存可以解决问题。

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