How does one set up multiple accounts with separate databases for Django on one server?

后端 未结 2 1314
心在旅途
心在旅途 2021-01-01 04:57

What options are there for installing Django such that multiple users (each with an \"Account\") can each have their own database?

The semantics are fairly intuitive

相关标签:
2条回答
  • 2021-01-01 05:06

    The Django ORM doesn't provide multiple database support classes, but it is definitely possible - you'll have to write a custom manager and make a few other tweaks. Eric Florenzano has a great article with detailed code samples:

    http://www.eflorenzano.com/blog/post/easy-multi-database-support-django/

    0 讨论(0)
  • 2021-01-01 05:24

    The Django way would definitely be to have separate installations with their own database name (#1). #2 would involve quite a bit of hacking with the ORM, and even then I'm not quite sure it's possible at all.

    But mind you, you don't need a WHOLE new installation of all the site's models/views/templates for each user, just a new settings.py with all the appropriate paths to the common source files. Plus, to run all these installations in Apache, do it the way I do here:

    <VirtualHost 1.2.3.4>
            DocumentRoot /www/site1
            ServerName site1.com
            <Location />
                    SetHandler python-program
                    SetEnv DJANGO_SETTINGS_MODULE site1.settings
                    PythonPath "['/www'] + sys.path"
                    PythonDebug On
                    PythonInterpreter site1
            </Location>
    </VirtualHost>
    
    <VirtualHost 1.2.3.4>
            DocumentRoot /www/site2
            ServerName site2.com
            <Location />
                    SetHandler python-program
                    SetEnv DJANGO_SETTINGS_MODULE site2.settings
                    PythonPath "['/www'] + sys.path"
                    PythonDebug On
                    PythonInterpreter site2
            </Location>
    </VirtualHost>
    

    assuming you've got /www/site1/settings.py, www/site2/settings.py and so on...

    Of course, you now need to have a main site where people log in, that then redirects you to the appropriate site (here I've just put it as "site1.com", "site2.com", but you get the idea.)

    0 讨论(0)
提交回复
热议问题