Opened 11 years ago

Last modified 11 years ago

#20922 closed New feature

Allow customizing the serializer used by contrib.sessions — at Version 3

Reported by: gwahl@… Owned by: Tim Graham
Component: contrib.sessions Version: 1.5
Severity: Release blocker Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Tim Graham)

The django.contrib.sessions.backends.signed_cookies session backend should be written in a way that allows subclasses to use their own serializer implementation. This will allow using JSON instead of Pickle to serialize sessions.

For background, see #20444 and https://groups.google.com/d/topic/django-developers/YwlZ9m9k1bE/discussion.

Change History (3)

comment:1 by gwahl@…, 11 years ago

Has patch: set

Pull request: https://github.com/django/django/pull/1474

Question: Should this be mentioned in the docs for this backend? An example of a backend using JSON could be helpful.

comment:2 by Tim Graham, 11 years ago

Component: Uncategorizedcontrib.sessions
Severity: NormalRelease blocker
Triage Stage: UnreviewedAccepted
Type: UncategorizedNew feature

Yes, I actually have a more extensive patch in the works.

comment:3 by Tim Graham, 11 years ago

Description: modified (diff)
Owner: changed from nobody to Tim Graham
Status: newassigned
Summary: Signed Cookie Session Backend Should Support Using a JSON SerializerAllow customizing the serializer used by contrib.sessions

The plan is to introduce settings.SESSION_SERIAZLIER in the next 1.5.x release. It'll default to using pickle for backwards compatibility, but the default will switch to using JSON in 1.6. Pull request in progress.

An additional API has been proposed by @apollo13 to allow customizing the serializiers a bit easier (rather than dealing with subclassing the current serializers, having to possibly write mixins, etc.). For example, in the existing patch JSONMessagesSerializer could be replaced by a hook:

class SerializerHook(object):
  handles_variables = ['variable1', ...]

  def to_primitive(self, name, object):
      pass

 def from_primitive(self, name, object):
     pass

Then in settings.py you'd have another setting:

SESSION_SERIALIZER_HOOKS = ['django.contrib.messages.session_hook', ...]

This would allow 3rd party applications to provide simple hooks for their session stuff (although that should be rare since you generally don't put that much logic into sessions, messages are one example of where you still might wanna do it).

Feedback on whether or not this additional complexity is worthwhile would be appreciated.

Note: See TracTickets for help on using tickets.
Back to Top