I have a legacy system that I’d like to extend with an application written in Django. The system hosts hundreds of small websites on the same server. A Mysql server takes care of the data: each website has its own Mysql database, (name derived from the hostname) with identical table structure. The list of all sites (hostnames and database names) is managed in a table in a separate database.
I don’t need the Django user/session tables at all, or the Django admin module.
Is it possible to write Django code so that:
- each Mysql query gets a correct database name (ultimately based on the request’s
Hostheader) - I don’t have to hardcode the hostname list in eg.
settings.py. When a website is added or renamed, everything “just works” (or maybe I can send a signal to reload the site list).
Django spports multi-db, that means, you can use more than one DB on a single project.
Documentation is here.
On the other hand, Your second need might not be possible in simple ways. Somehow, you must add a new DB description to
settings.pyfile. There is one possible way o solve this, but it is not a good one in some ways…When you define a new web site (from an UI interface like a webpage), you can write a script to open
settings.pyand add your new db information to DATABASE schema and save it and reload your settings… But as i said, writingsettings.pyprogramatically is dangerous.