Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

What seems to fix the issue - add to settings.py:

import sys
reload(sys) #this brings method setdefaultencoding
sys.setdefaultencoding('utf-8')

However I find it strange how django memcached cache backend works:

>>> from django.core.cache import cache
>>> cache.set('key', u'\u1537')
>>> cache.get('key')
'\xe1\x94\xb7'

Note that the value comes back as encoded string, not as original value.

With Redis cache (and similarly with the LocMemCache) I get what I'd expect:

>>> cache.set('key', u'\u1537')
True
>>> cache.get('key')
u'\u1537'

Redis cache also works irrespective of the default encoding.

Because of that I would recommend to use django-redis-cache.

What seems to fix the issue - add to settings.py:

import sys
reload(sys) #this brings method setdefaultencoding
sys.setdefaultencoding('utf-8')

However As @Tvartom suggested - this issue is seen when the django memcache caching backend is used. I find it strange how django memcached cache backend works:works (even after the default encoding is set to utf-8):

>>> from django.core.cache import cache
>>> cache.set('key', u'\u1537')
>>> cache.get('key')
'\xe1\x94\xb7'

Note that the value comes back as encoded string, not as original value.

With Redis cache (and similarly with the LocMemCache) I get what I'd expect:

>>> cache.set('key', u'\u1537')
True
>>> cache.get('key')
u'\u1537'

Redis cache also works irrespective of the default encoding.

Because of that I would recommend to use django-redis-cache.