Skip to content

Example


Here follows an example in how to use django-tenants-url.

Table of Contents


Initial Notes

This is a very basic example how to see the UUID of a tenant and use it in the headers. For more robust and secure approach we also provide views, serializers, utils and some permissions and handlers that can be used accordingly.

Install package

pip install django-tenants-url

Settings

After installing django-tenants-url, add it into the settings.

INSTALLED_APPS = [
    ...
    'django_tenants',
    'django_tenants_url',
    ...
]

Create Tenant django app

Let's create a tenant custom app and add it into the settings.

  1. Create the app
django-admin startapp tenant
  1. Add the app to the INSTALLED_APPS.

  2. python INSTALLED_APPS = [ ... 'django_tenants', 'django_tenants_url', 'tenant', ... ]

More settings will be needed but we will back later on to update the settings.

Create the models

  1. Inside the newly created tenant in the models.py.
from django_tenants_url.models import TenantMixin, DomainMixin, TenantUserMixin


class Client(TenantMixin):
    pass


class Domain(DomainMixin):
    pass


class TenantUser(TenantUserMixin):
    pass

Create and run migrations

  1. Create migrations.
python manage.py makemigrations
  1. Run migrations.
python manage.py migrate_schemas

Update settings

With the models created we can now update the settings.py.

  1. Add extra needed settings.
INSTALLED_APPS = [...]

TENANT_MODEL = 'tenant.Client' # needed from django-tenants

TENANT_DOMAIN_MODEL = 'tenant.Domain' # needed from django-tenants

# DTU_TENANT_USER_MODEL
DTU_TENANT_USER_MODEL = 'tenant.TenantUser' # unique to django-tenants-url
  1. Update the MIDDLEWARE.
MIDDLEWARE = [
    'django_tenants_url.middleware.RequestUUIDTenantMiddleware',
    'django.middleware.security.SecurityMiddleware',
    ...
]

Start the server

Once the first request hits the server, it should create the public tenant and public domain.

Get a Tenant UUID

django-tenants-url provides out-of-the-box views to help you with all of the process as well as some functions that can be used to get some of the information like the UUID needed to be used in the header of a request and map to user schema.

from django_tenants_url.utils import get_tenants

tenants = get_tenants()
print(tenants)

[
    {
        "id": 1,
        "tenant_name": "Public",
        "tenant_uuid": "a66b19e4-3985-42a1-87e1-338707c4a203"
    }
]

Use the UUID within the headers

Using the uuid a66b19e4-3985-42a1-87e1-338707c4a203 we can now use the header that maps the user with the schema.

curl --header "X_REQUEST_ID: a66b19e4-3985-42a1-87e1-338707c4a203" -v http://localhost:8000/my-view