March 22, 2010

Django: One application, multiple instances

A current project requires to be able to have multiple instances of a single web application application running in different process and with segregated databases. In the meantime, the core of the application should not be duplicated.

A solution to this problem using Django is to create 'Shell projects'. Their goal is to import the core application settings, and to overload only the required parts : right now the database file. Each shell project live in its own folder.

$ django-admin.py startproject core
# write the application...


All the application is implemented and tested in core. In the instance1 folder we only find the overloading of parts of the core application.

$ create_shell_application.sh instance1
$ ls instance1
__init__.py* manage.py* settings.py*
$ more instance1/settings.py
from core.settings import *
DATABASE_NAME = 'instance1.sqlite3'
# + instance1 specific configuration if needed...


Starting the instance1 application (with ./manage.py runserver) makes it run with the common code, but its own database instance. And one can have as many instances as needed. The core application is to be packaged as an Egg and installed on the target laptops.