Plio uses the django-tenants package to implement multitenancy.
This guide aims to provide details on how Plio is using multi-tenancy and pre-requisites for someone contributing to the code.
Run the following commands to create an organization from programmatically:
-
Get into python shell.
python3 manage.py shell
-
Create a public tenant that will be the default one.
Note: Public tenant is already created when docker container is initialized through Django fixtures. Please refer
entrypoint.sh
andorganizations/fixtures/default_tenant.yaml
files.# create your public tenant from organizations.models import Organization, Domain tenant = Organization(schema_name='public', name='Plio', shortcode='plio') tenant.save() domain = Domain() domain.domain = 'plio.in' # use domain.domain = '0.0.0.0' for development environment domain.tenant = tenant domain.is_primary = True domain.save()
-
Create a tenant organization that will have it's own schema.
# create your first real tenant tenant = Organization(name='Avanti Fellows', shortcode='af') tenant.save() domain = Domain() domain.domain = 'plio.in/avantifellows' domain.tenant = tenant domain.is_primary = True domain.save()
-
Now log into your PostgreSQL database server. Run the following command to list all the schemas. You will see various schemas along with the two above:
public
andavantifellows
SELECT schema_name FROM information_schema.schemata;
-
List tables from
public
andavantifellows
schemas. You will notice that both schemas have basic tables for plio related things but public schema will also have tables for organization and user.-- view tables in public schema SELECT tablename FROM pg_catalog.pg_tables where schemaname='public'; -- view tables in tenant organization schema SELECT tablename FROM pg_catalog.pg_tables where schemaname='generated_schema_name';