[Python] Dynamically Create and Use Django Database Connections for Each School Without Server...

Discussão em 'Python' iniciado por Stack, Março 12, 2025.

  1. Stack

    Stack Membro Participativo

    I am developing a Django application where schools can register, and each school has its own database. However, I am facing an issue: after creating a new database for a school, the Django server needs to be restarted for the new database to be recognized.

    I want to dynamically create database connections for each school without having to restart the Django server. I've tried various approaches, including creating dynamic connections using django.db.connections, but it doesn't seem to work as expected.

    Is there a solution or recommended approach to allow Django to dynamically create and use database connections for each new school without requiring a server restart?

    Thanks in advance for any help or suggestions!

    ConnectionDoesNotExist at /inscription/ecole/

    The connection 'school_db_belle_rose' doesn't exist.

    Request Method: POST
    Request URL: http://127.0.0.1:8000/inscription/ecole/
    Django Version: 5.0
    Exception Type: ConnectionDoesNotExist
    Exception Value:

    The connection 'school_db_belle_rose' doesn't exist.

    Exception Location: C:\Users\Ppang\Desktop\project_procode\ConnectEdu\venv\Lib\site-packages\django\utils\connection.py, line 61, in __getitem__
    Raised during: comptes_ecole.views.InscriptionEcole
    Python Executable: C:\Users\Ppang\Desktop\project_procode\ConnectEdu\venv\Scripts\python.exe
    Python Version: 3.11.0
    Python Path:

    ['C:\\Users\\Ppang\\Desktop\\project_procode\\ConnectEdu\\back-end',
    'C:\\Python311\\python311.zip',
    'C:\\Python311\\DLLs',
    'C:\\Python311\\Lib',
    'C:\\Python311',
    'C:\\Users\\Ppang\\Desktop\\project_procode\\ConnectEdu\\venv',
    'C:\\Users\\Ppang\\Desktop\\project_procode\\ConnectEdu\\venv\\Lib\\site-packages']

    Server time: Mon, 08 Jan 2024 02:22:52 +0000

    Continue reading...

Compartilhe esta Página