python - 为什么我会收到此 PicklingError?

标签 python django pickle wagtail

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

在网址中:

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

在 View 中:

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 对象。这可能有什么关系吗?

最佳答案

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

我建议使用 pickle 兼容的对象类型。另一方面在Python documentation有一些关于如何实现/自定义 pickling/unpickling 逻辑的指南。

关于python - 为什么我会收到此 PicklingError?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50706311/

相关文章:

Python 2.6 文件存在 errno 17,

python - 如何对双击按键进行编程以使非修饰键在程序中的行为类似于修饰键?

python - 为什么要在Python的虚拟环境中创建requirements.txt文件?

python - Django 1.5 的多种用户类型、自定义字段和共享身份验证

python - pickle 后如何更改类方法的定义

python - 初始化后无法访问另一个类中的实例

django - Heroku Django 应用程序静态文件未提供服务(错误 OSError : [Errno 2])

html - 代理浏览器(opera mini 和 UC 浏览器)中不显示背景图像

python - Pickle.dump 到变量

python : How to read pickle dump?