04/28: sessions in django
I was looking at django sessions and was a bit confused until I read the source code.
django/contrib/sessions/models.py django/contrib/sessions/backends/base.py django/contrib/sessions/backends/db.py django/contrib/sessions/backends/cache.py django/contrib/sessions/backends/cached_db.py django/contrib/sessions/backends/file.py
You might think the interesting file to look at is django/contrib/sessions/models.py, but really the “toplevel” session object is defined in base.py. The object in base.py SessionBase. It is a base class for the various session implementations.
If you’re using a database-backed SessionStore, then you’ll be using base.py, db.py and session.py. db.py uses session.py for the database model and database interaction. In a view, the session object in the request object that is supplied by the session middleware is actually a SessionStore object. That is the object that has the methods get_expiry_age, etc.
I wanted to get some info out of every session in a batch mode including expiry age, so I needed to traverse SessionStore.objects.all(), not Session.objects.all().