Skip to content

Database registration

django_db_tenants.db adds tenant connections to django.db.connections while the process runs. See Runtime database registration for the mechanism.

Use it

The middleware, tenant_context() and the provisioning functions call ensure_db_registered() for you. Call it yourself to use .using() or connections[...] directly:

from django.db import connections
from django_db_tenants import ensure_db_registered
from django_db_tenants.registry import get_client

alias = ensure_db_registered(get_client("acme"))  # "acme"
with connections[alias].cursor() as cursor:
    cursor.execute("SELECT count(*) FROM auth_user")
Function Effect
ensure_db_registered(client, force=False) Registers client.slug with the settings returned by the client's database strategy, unless this process already has it at the same config version. force=True re-registers anyway. Returns the alias
is_registered(alias) Whether the alias is in connections.settings (for any reason, including aliases from DATABASES)
unregister_db(alias) Closes this thread's connection for the alias and removes it from this process

Registration is per process and thread-safe; closing the connection only affects the calling thread. Other threads drop their connection at the end of their request, as usual.

django_db_tenants.db

Runtime registration of tenant databases.

Django consults connections.databases (the live settings dict) every time it opens a connection, and connections are created lazily. Adding an entry to that dict is therefore enough to make a new alias usable — no restart required.

The connection settings come from the client's database strategy (client.db_strategy, see strategies.py).

Registration is per process: each worker registers an alias the first time it needs it. config_version() lets a process notice that a client's connection details changed (e.g. password rotation) and re-register.

is_registered

is_registered(alias: str) -> bool

ensure_db_registered

ensure_db_registered(client, force: bool = False) -> str

Make client.alias usable with connections[alias] / .using(alias).

unregister_db

unregister_db(alias: str) -> None