python - 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?
解决方案
我认为这是与缓存相关的(最近有同样的问题),其中一些对象不能被腌制以进行缓存,并且您已经确认您正在使用缓存。所以禁用缓存可以解决问题。
我建议使用 pickle 兼容的对象类型。另一方面,在Python 文档中,有一些指导方针如何实现/自定义酸洗/解酸逻辑。
推荐阅读
- linux - 奇怪的 ssh 行为 - ssh 在未知位置设置 PROMPT_COMMAND
- html - 我的孩子 Div 无法滚动到底部
- java - @Transactional 不提交(Spring Mybatis)
- vue.js - 在其他组件中侦听后无法识别通过发射的Vue对象
- java - 如何从 Vaadin 8 中的子线程访问 UI 线程
- html - 我的 imageContainer div 的高度比它包含的元素略大,这使得它看起来好像顶部和底部 div 之间有一个空间
- sql - 如何解析 JSON 属性而不显式引用它们?
- bash - sed 在两个模式之间,其中结果包含第三个模式
- c# - 根据位置和长度创建文本文件
- android - 如何在 Android Studio (MacOS High Sierra) 中增加字体大小?