Joe Jasinski
Joe Jasinski

Reputation: 10801

Extending the Session Middleware

I'm using Django 1.3, and I'd like to modify the session middleware to add some methods that will help me out.

In ./settings.py

SESSION_ENGINE='tcore.my_sessions.MySessionStore'

In ./tcore/my_sessions.py:

from django.contrib.sessions.backends.db import SessionStore
from django.contrib.sessions.middleware import SessionMiddleware
from django.conf import settings

class MySessionStore(SessionStore):    
    ....
    def my custom methods here

However, I keep getting some weird exceptions when I do this. What's the proper way to make a custom Session Engine?

AttributeError at /create/
type object 'MySessionStore' has no attribute 'SessionStore'
Request Method: GET
Request URL:    http://localhost:8000/create/
Django Version: 1.3.1
Exception Type: AttributeError
Exception Value:    
type object 'MySessionStore' has no attribute 'SessionStore'

Python Version: 2.6.1

Upvotes: 3

Views: 4395

Answers (1)

jpic
jpic

Reputation: 33420

Looking at the documentation of SESSION_ENGINE, take such an example: django.contrib.sessions.backends.file. The source of this module defines a SessionStore class. So that's what you should do too:

./tcore/my_sessions.py:

from django.contrib.sessions.backends.db import SessionStore as DbSessionStore

class SessionStore(DbSessionStore):
    def __init__(self, *args, **kwargs):
        print 'hello from SessionStore'
        super(SessionStore, self).__init__(*args, **kwargs)

settings.py:

SESSION_ENGINE='tcore.my_sessions'

Upvotes: 4

Related Questions