Skip to content


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


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


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
from django_tenants_url.models import TenantMixin, DomainMixin, TenantUserMixin

class Client(TenantMixin):

class Domain(DomainMixin):

class TenantUser(TenantUserMixin):

Create and run migrations

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

Update settings

With the models created we can now update the

  1. Add extra needed settings.

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

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

DTU_TENANT_USER_MODEL = 'tenant.TenantUser' # unique to django-tenants-url
  1. Update the MIDDLEWARE.

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()

        "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