Package: django-tenants-url
Django Tenants URLOfficial Documentation-https://tarsil.github.io/django-tenants-url/Table of ContentsDjango Tenants URLTable of ContentsAbout Django Tenants URLDependenciesMotivationInstallationAfter installing django-tenantsInstall django-tenants-urlDjango Tenants URL SettingsX_REQUEST_IDExampleDocumentation and SupportLicenseAbout Django Tenants URLDjango Tenants URL is a wrapper on the top of thedjango-tenantspackage that serves a different
yet common use case, the multi-tenant implementation via HEADER and not usingsub domains.A special thanks to the team behindDjango Tenants.DependenciesThe project containsviews,permissions,modelsand more addons that can be used across projects.django-tenants-urlis built on the top of the following dependencies:DjangoDjango Rest FrameworkDjango TenantsMotivationWhen implementing multi tenancy architecture there are many factors to consider and cover and
those were greatly approached bydjango-tenantsbut 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-urlis a wrapper on the top ofdjango-tenantsand 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.comMy Companyis the tenant of thesaascompany.comthat is publicaly available to the users.
When themycompanyis 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 yourapache,nginxor any other configurations
that accepts and forwards calls to the*.sasscompany.comand 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 usewww.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 urlInstallationPrior to install thedjango-tenants-url,django-tenantsneeds to be installed
as well. Please follow the installation steps fromDjango TenantsAfter installing django-tenantsInstall django-tenants-urlpipinstalldjango-tenants-urlTheTENANT_MODELandTENANT_DOMAIN_MODELfromdjango-tenantsneed to be also in thesettings.py.Adddjango-tenants-urltoINSTALLED_APPS.INSTALLED_APPS=[...'django_tenants','django_tenants_url',...]django-tenants-urloffers a special wrapper over themixinsofdjango-tenantswith the same names so you don't need to worry about breaking changes and the
additional uniqueTenantUserMixinthat maps the users with a tenant.Create the models.# myapp.models.pyfromdjango.dbimportmodelsfromdjango_tenants_url.modelsimportTenantMixin,DomainMixin,TenantUserMixinclassClient(TenantMixin):"""This table provides the `tenant_uuid` neededto be used in the `X_REQUEST_HEADER` and consumedby the RequestUUIDTenantMiddleware."""passclassDomain(DomainMixin):passclassTenantUser(TenantUserMixin):passAdd theDTU_TENANT_USER_MODELtosettings.py.# settings.py...DTU_TENANT_USER_MODEL='myapp.TenantUser'...Update theMIDDLEWAREto have the newRequestUUIDTenantMiddleware.
Preferentially at the very top.# settings.py...MIDDLEWARE=['django_tenants_url.middleware.RequestUUIDTenantMiddleware','django.middleware.security.SecurityMiddleware',...]...Generate the migrations.pythonmanage.pymakemigrationsRun the migrations as if it wasdjango-tenantsand not the classicmigrate.pythonmanage.pymigrate_schemasTheUUIDneeded for theRequestUUIDTenantMiddlewarecan be found in your
table inherited from theTenantMixin.None: Do not runpython manage.py migrateor else it will sync everything into the public.And that is it. TheRequestUUIDTenantMiddlewareshould be able to map
theTenantUsercreated with a tenant and route the queries to the associated schema.Checkout thedocumentationand understand how to integrate with your views and taking advantage
of the utils for yourTenantUser(or your implementation),Django Tenants URL Settings# default settingsDTU_TENANT_NAME="Public"DTU_TENANT_SCHEMA="public"DTU_DOMAIN_NAME="localhost"DTU_PAID_UNTIL="2100-12-31"DTU_ON_TRIAL=FalseDTU_HEADER_NAME="HTTP_X_REQUEST_ID"DTU_AUTO_CREATE_SCHEMA=TrueDTU_AUTO_DROP_SCHEMA=FalseDTU_TENANT_USER_MODEL=NoneX_REQUEST_IDBy defaultdjango-tenants-urlhas the header nameHTTP_X_REQUEST_IDthat will be lookup
from the middleware when sent via HTTP.
This name can be overriten by the special settingDTU_HEADER_NAME.ExampleA simple example can be foundhere.Another Django Like app implementing Django Tenants Url can be foundhereDocumentation and SupportFull documentation for the project is available athttps://tarsil.github.io/django-tenants-url/LicenseCopyright (c) 2022-present Tiago Silva and contributors under theMIT license.