django-qsessions is a session backend for Django that extends Django's cached_db
session backend
and Session
model to add following features:
- Sessions have a foreign key to User
- Sessions store IP and User Agent
Please note that if your system is in production and there are lots of active sessions using another session backend, you need to migrate sessions manually.
First, make sure you've configured your cache. If you have multiple caches defined in
CACHES
, Django will use the default cache. To use another cache, setSESSION_CACHE_ALIAS
to the name of that cache.Install the latest version from PyPI:
pip install django-qsessions
In settings:
- In
INSTALLED_APPS
replace'django.contrib.sessions'
with'qsessions'
. - In
MIDDLEWARE
orMIDDLEWARE_CLASSES
replace'django.contrib.sessions.middleware.SessionMiddleware'
with'qsessions.middleware.SessionMiddleware'
. - Add
SESSION_ENGINE = 'qsessions.backends.cached_db'
.
- In
Run migrations to create
qsessions.models.Session
model.python manage.py migrate qsessions
For enabling location detection using GeoIP2 (session.location
):
Install
geoip2
package:pip install geoip2
Set
GEOIP_PATH
to a directory for storing GeoIP2 database.Run the following command to download latest GeoIP2 database. You can add this command to a cron job to update GeoIP2 DB automatically.
python manage.py download_geoip_db
For clearing expired sessions from DB, run python manage.py clearsessions
. It's recommended to
add it to a daily cron job.
django-user-sessions has the same functionality,
but it's based on db
backend. Using a cache will improve performance.
We got ideas and some codes from django-user-sessions. Many thanks to Bouke Haarsma for writing django-user-sessions.
- Write better documentation.
- Explain how it works (in summary)
- Explain how to query sessions based on users, and delete sessions
- Add more details to existing documentation.
- Write tests (for latest Python and Django versions)
- Performance benchmark (and compare with Django's cached_db)
Contributions are welcome!
MIT