[Django]-Python 2.7 and 3.7.2 compatible django-redis serializer

3👍

If anyone is having this issue, I came to this solution

# -*- coding: utf-8 -*-
import six
from django_redis.serializers.pickle import PickleSerializer, pickle


class CompatPickleSerializer(PickleSerializer):

    def loads(self, value):
        if six.PY3:
            return self._loads_py3(value)
        return super(CompatPickleSerializer, self).loads(value)

    def _loads_py3(self, value):
        return pickle.loads(
            value,
            fix_imports=True,
            encoding='latin1'
        )

I changed from encoding='bytes' to latin1 and it worked. No need to force_bytes.

Another important thing is that you need to force the PICKLE_VERSION to 2, otherwise you won’t be able to pickle the data on python2.7 if it was serialized on python3.

Hope it helps anyone.

Leave a comment