Django Session Sharing


We have multiple django apps. One or more of them don't seem to belong in the same project. We thus have multiple django projects to host them. This is fine, but it'd be great to be able to have the session table be shared across projects.


Here are the possible solutions I've come up with. I am most familiar with mysql, so I am assuming that mysql is being used as the database backend.

I favor handling this situation at the database level. There really wouldn't be interference between the different django projects.

Common Database

The simplest solution to implement is to have both projects configured to use the same database. This is fine as long as we are aware of this. Any tables will be visible to the other django project. In our case, we aren't using the django auth system for one of the apps, thus the only common table will be the session table. This has the advantage of being portable across basically every database backend that django supports.

Unfortunately this won't work in every case.

Two databases, shared table

The way to more or less get a shared table is to create a view.

A view is more or less a stored query that acts like a table.

The syntax is simple:

use djangodb;
CREATE VIEW django_sessions as select * FROM otherdjangodb.django_sessions;

The view works and acts just like a table. You can select from and insert into it.

Replicate to another server

Don't know if it is possible.

The idea would be to set up server replication restricted to the table level. The database on both servers would be different, but there would need to be two-way replication for the django_sessions table.

Patch Django

If implementing a fix at the database level isn't an option, you can modify django. As of writing, django doesn't support multiple databases in the backend. You would be able to tweak the one app, django.contrib.sessions. There are efforts to add multiple database support with the intent of merging those efforts into trunk.