5

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?

Pablo Viacheslav
  • 873
  • 1
  • 8
  • 15

1 Answers1

2

I think this is cache related (had same issue lately), where some objects can't be pickled for caching, and as you've confirmed you are using cache. So disabling the cache would solve the problem.

I suggest to use pickle compatible object types. On the other hand in Python documentation there is some guidelines how to implement / customize the pickling / unpickling logic.