I am trying to create MultiTenant Application.

i want to create on function in which i can pass my connection details and connect to that database.
this is the current connection details which i am getting from

from django.db import connection
 {'ENGINE': 'django.db.backends.mysql', 'NAME': 'S1', 'USER': 'wtc', 'PASSWORD': 'Black@97', 'HOST': 'localhost', 'PORT': 3306, 'ATOMIC_REQUESTS': False, 'AUTOCOMMIT': True, 'CONN_MAX_AGE': 0, 'OPTIONS': {}, 'TIME_ZONE': None, 'TEST': {'CHARSET': None, 'COLLATION': None, 'MIGRATE': True, 'MIRROR': None, 'NAME': None}}

now let say i want to connect to database S2. but my below code is not working.

# Switch the database connection
        print('called', connection.settings_dict)
        connection.disconnect()
        connection.settings_dict['NAME'] = 'scgtplatform'
        print('connected')

it says AttributeError: ‘DatabaseWrapper’ object has no attribute ‘disconnect’

Nope, Django doesn’t work that way. “You” don’t control or manage your connection, Django does.

It is possible to add connections to the DATABASES settings, but it’s not going to be “persistent” and you need to check that the connection exists every time you try to use it. But it can be done.

See the thread at Multiple dynamic database access