Django switch database dynamically

前端 未结 1 1765
别那么骄傲
别那么骄傲 2021-02-11 06:08

I\'d like to switch databases upon user login. I\'ve created this login signal.. but it doesn\'t work

from django.dispatch import receiver
from django.contrib.au         


        
相关标签:
1条回答
  • 2021-02-11 06:46

    It's the wrong way of doing it.

    Honestly I don't think there is a straightforward, stable way of doing this in Django. It's just not designed for it.

    Instead, I'd set up a settings_username.py file for each user, which specifies a secondary database called personal or something. Then, after logging, have them redirect to a new domain, like username.example.com, which uses a unique .wsgi file that pulls in the settingsusername.py file.

    Now, as far as the system is concerned, each website is totally separate and unique to that user. Just make sure to set the session cookie to example.com so that they're still logged in when they go to their user website.

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