Search code examples
pythondjangomulti-tenant

Access public schema from tenant schema, Django-Tenant


I'm currently using Django-Tenant-schema for a multy tenancy application. Is there a way to access the public schema when working with the tenant schemas? I can access a tenant database from the public with this line:

with tenant_context(tenant):

but when I am in a django application using only the tenant_schema how can I access my Client model in my public schema?

thanks!


Solution

  • Well, you can do that by adding the app that contains your client model to your SHARED_APPS settings. After this, you can create a user under the public schema using 'schema_context' as follows:

    from tenant_schemas.utils import schema_context
    with schema_context('public'):
        # create user
    

    You can also access your client model from your public schema by creating a superuser in your public schema. Try...

    $ python manage.py tenant_command createsuperuser --schema=public
    

    And login to your public tenant admin from which you have full access to your client model in public schema.

    Note: before creating a user in your public schema, you have to first create the public schema using your client model as stated in https://django-tenants.readthedocs.io/en/latest/use.html#creating-a-tenant

    Hint: The 'shell' command will help you in creating the schema (public).