Shell¶
Since all DB queries made via any of the ORM libraries used by the app, would now need to be bound to a tenant context, by default, any query run from within the shell would fail.
In order to fix this, some tenant context has to be set explicitly as follows:
$ python manage.py shell
>>> from tenant_router.threadlocal import tls_tenant_manager
>>> from tenant_router.managers import tenant_context_manager
>>> from tenant_router.context_decorators import tenant_context_bind
>>> tls_tenant_manager.push_tenant_context(
... tenant_context_manager.get_by_id('some_tenant_id'))
>>> ...some queries with some_tenant_id as context...
>>> with tenant_context_bind('some_other_tenant_id'):
>>> ... some more queries with some_other_tenant_id as context
>>> quit()
Warning
For Django ORM, if reserved aliases have been defined, then it is possible to execute queries pertaining to the models which are linked to those aliases without setting a tenant context but this is highly not recommended as this could cause unnecessary confusion around why a few queries would fail but others would succeed.