Django Tenants managed by a single URL.
Project description
Django Tenants URL
Official Documentation - https://tarsil.github.io/django-tenants-url/
Table of Contents
About Django Tenants URL
Django Tenants URL is a wrapper on the top of the django-tenants
package that serves a different
yet common use case, the multi-tenant implementation via HEADER and not using sub domains
.
A special thanks to the team behind Django Tenants.
Dependencies
The project contains views, permissions, models and more addons that can be used across projects.
django-tenants-url
is built on the top of the following dependencies:
Motivation
When implementing multi tenancy architecture there are many factors to consider and cover and those were greatly approached by django-tenants but so summarize, there are 3 common ways:
- Shared schemas - The data of all users are shared within the same schema and filtered by common IDs or whatever that is unique to the platform. This is not so great for GDPR.
- Shared database, different Schemas - The user's data is split by different schemas but live on the same database.
- Different databases - The user's data or any data live on different databases.
As mentioned before, django-tenants-url
is a wrapper on the top of django-tenants
and therefore we will be approaching the second.
Many companies have limited resources (money, people...) and limited choices from those constraints. When implementing multi tenancy, the default would be to use subdomains in order to access the desired schema. E.g.:
www.mycompany.saascompany.com
My Company
is the tenant of the saascompany.com
that is publicaly available to the users.
When the mycompany
is sent to the backend, the middleware splits the subdomain and
the TLD (top-level domain) and maps the tenant with the schema associated.
For this work, one of the ways is to change your apache
, nginx
or any other configurations
that accepts and forwards calls to the *.sasscompany.com
and performs the above action.
If the frontend and backend are split, extra configurations need also to be made on that front and all of this can be a pain.
What does django-tenants-url solve?
The principle of mapping users to the schemas remains the same but the way of doing it
is what diverges from the rest. What if we were able to only use www.sasscompany.com
,
login as usual and automatically the platform knows exactly to which schema the user
needs to be mapped and forward?
This is what django-tenants-url solves. A single url that does the multi tenancy without breaking the principle and architecture and simply using one single url
Installation
Prior to install the django-tenants-url
, django-tenants
needs to be installed
as well. Please follow the installation steps from
Django Tenants
After installing django-tenants
Install django-tenants-url
pip install django-tenants-url
-
The
TENANT_MODEL
andTENANT_DOMAIN_MODEL
fromdjango-tenants
need to be also in thesettings.py
. -
Add
django-tenants-url
toINSTALLED_APPS
.INSTALLED_APPS = [ ... 'django_tenants', 'django_tenants_url', ... ]
-
django-tenants-url
offers a special wrapper over themixins
ofdjango-tenants
with the same names so you don't need to worry about breaking changes and the additional uniqueTenantUserMixin
that maps the users with a tenant. -
Create the models.
# myapp.models.py from django.db import models from django_tenants_url.models import TenantMixin, DomainMixin, TenantUserMixin class Client(TenantMixin): """ This table provides the `tenant_uuid` needed to be used in the `X_REQUEST_HEADER` and consumed by the RequestUUIDTenantMiddleware. """ pass class Domain(DomainMixin): pass class TenantUser(TenantUserMixin): pass
-
Add the
DTU_TENANT_USER_MODEL
tosettings.py
.# settings.py ... DTU_TENANT_USER_MODEL = 'myapp.TenantUser' ...
-
Update the
MIDDLEWARE
to have the newRequestUUIDTenantMiddleware
. Preferentially at the very top.# settings.py ... MIDDLEWARE = [ 'django_tenants_url.middleware.RequestUUIDTenantMiddleware', 'django.middleware.security.SecurityMiddleware', ... ] ...
-
Generate the migrations.
python manage.py makemigrations
-
Run the migrations as if it was
django-tenants
and not the classicmigrate
.python manage.py migrate_schemas
-
The
UUID
needed for theRequestUUIDTenantMiddleware
can be found in your table inherited from theTenantMixin
.
None: Do not run python manage.py migrate
or else it will sync everything into the public.
And that is it. The RequestUUIDTenantMiddleware
should be able to map
the TenantUser
created with a tenant and route the queries to the associated schema.
Checkout the documentation
and understand how to integrate with your views and taking advantage
of the utils for your TenantUser
(or your implementation),
Django Tenants URL Settings
# default settings
DTU_TENANT_NAME = "Public"
DTU_TENANT_SCHEMA = "public"
DTU_DOMAIN_NAME = "localhost"
DTU_PAID_UNTIL = "2100-12-31"
DTU_ON_TRIAL = False
DTU_HEADER_NAME = "HTTP_X_REQUEST_ID"
DTU_AUTO_CREATE_SCHEMA = True
DTU_AUTO_DROP_SCHEMA = False
DTU_TENANT_USER_MODEL = None
X_REQUEST_ID
By default django-tenants-url
has the header name HTTP_X_REQUEST_ID
that will be lookup
from the middleware when sent via HTTP.
This name can be overriten by the special setting DTU_HEADER_NAME
.
Example
A simple example can be found here.
Another Django Like app implementing Django Tenants Url can be found here
Documentation and Support
Full documentation for the project is available at https://tarsil.github.io/django-tenants-url/
License
Copyright (c) 2022-present Tiago Silva and contributors under the MIT license.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
File details
Details for the file django_tenants_url-1.0.1-py3-none-any.whl
.
File metadata
- Download URL: django_tenants_url-1.0.1-py3-none-any.whl
- Upload date:
- Size: 14.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b7a97c5ef5a2c51582aaf6f898c9f8dcb7e737515c064f4b8518ba219fe3fb1a |
|
MD5 | 7d5a95358560615f3041e77bc7ffa885 |
|
BLAKE2b-256 | 9a7bd574d4abea59a42a96fdc02a090ea181953074218e022cd2977d7cfbceb9 |