Opened 10 years ago

Closed 10 years ago

#23596 closed Bug (invalid)

request.session.save() raise TypeError: [<Group: Admin user>] is not JSON serializable

Reported by: Cao T Owned by: nobody
Component: contrib.sessions Version: 1.7
Severity: Normal Keywords: session save serializer dumps JSON serializable
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: yes

Description

request.session.save() raise TypeError: [<Group: Admin user>] is not JSON serializable

it's the auth.models.Group.objects.all() that is not JSON serializable. it worked perfect well upto django 1.5.

Django 1.5:

    def encode(self, session_dict):
        "Returns the given session dictionary pickled and encoded as a string."
        pickled = pickle.dumps(session_dict, pickle.HIGHEST_PROTOCOL)
        hash = self._hash(pickled)
        return base64.b64encode(hash.encode() + b":" + pickled).decode('ascii')

Django 1.7:

    def encode(self, session_dict):
        "Returns the given session dictionary serialized and encoded as a string."
        serialized = self.serializer().dumps(session_dict)
        hash = self._hash(serialized)
        return base64.b64encode(hash.encode() + b":" + serialized).decode('ascii')

it looks there is some issue with self.serializer().

Change History (1)

comment:1 by Simon Charette, 10 years ago

Resolution: invalid
Status: newclosed

For documented security reasons sessions are serialized using JSON instead of pickle since Django 1.6.

If you want to store a collection of model instances using django.contrib.sessions.serializers.JSONSerializer I suggest you convert it to a list of primary keys instead:

sessions['groups'] = [group.pk for group in groups]
Note: See TracTickets for help on using tickets.
Back to Top