Skip to main content

Django Shared Schema Multi Tenant

Project description

Multi Tenant App for Django

Shared approach multi tenancy system for Django Projects.

Installation

Add accrete to installed_apps and accrete.middleware.TenantMiddleware to middleware

TenantModel

Base Model with a ForeignKey to tenant.Tenant and objects set to accrete.models.TenantManager

Basic Usage

from accrete.models import TenantModel

class MyModel(TenantModel):
    ...

Each user can be a member of multiple Tenants, just add them to tenant.members.
This is necessary when using the Mixins and Middleware provided by this app.

Middleware

tenant.middleware.TenantMiddleware

This Middleware adds the Tenant(request.tenant) and Member(request.member) objects as attributes to the request object and sets a cookie with the tenant_id.

If the user is a member of multiple tenants, the request is parsed for a tenant_id in this order.

  • The "tenant" parameter in the POST data
  • The Header X-TENANT-ID
  • The "tenant_id" URL Parameter in the GET data
  • The "tenant_id" cookie previously set by the Middleware

If no tenant could be assigned the two attributes are set to None.
Additionally, the user is checked for membership of the found tenant.

Adds the tenant to the POST data.

The Middleware must be added to the MIDDLEWARE setting after your authentication Middleware as it needs access to request.user.is_authenticated().

Views

Mixins

tenant.views.TenantRequiredMixin

Adds tenant and optionally access right checks to the dispatch method.
This Mixin is meant as a substitute to django.contrib.auth.mixins.LoginRequiredMixin as TenantRequiredMixin inherits LoginRequiredMixin.

Adds the member_access_groups attribute to the view. This attribute can be a list of codes from the tenant.models.AccessGroup model. If present, the member must be part of one of the listed groups to access the view. Failing checks are handled in a similar way as in LoginRequiredMixin.

Decorators

tenant.decorators.tenant_required

Substitute for django.contrib.auth.decorators.login_required
Checks if a tenant is set on the request and redirects to the TENANT_MISSING_URL specified in the settings.

The decorator itself is wrapped by login_required and can pass the arguments redirect_field_name and login_url to login_required.

Forms

tenant.forms.Form

Form class that adds the tenant as a field and filters the queryset of every field that has a queryset attribute.

tenant.forms.ModelForm

Same behaviour as tenant.forms.Form.

Settings

Custom settings

  • TENANT_MISSING_URL
    Redirect to this URL when no tenant could be set for an authenticated user.

  • TENANT_MEMBER_NOT_AUTHORIZED_URL
    Redirect to this URL when a member tries to access a URL without having the needed access rights.

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 Distribution

accrete-0.0.154.tar.gz (1.4 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

accrete-0.0.154-py3-none-any.whl (1.5 MB view details)

Uploaded Python 3

File details

Details for the file accrete-0.0.154.tar.gz.

File metadata

  • Download URL: accrete-0.0.154.tar.gz
  • Upload date:
  • Size: 1.4 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.3

File hashes

Hashes for accrete-0.0.154.tar.gz
Algorithm Hash digest
SHA256 31f923229934c90f5e0511e2d90daf0183afaef2a4fa9d02be4cf8630e7dbe70
MD5 6ddd538db563dc83e3a0cb568a2d614f
BLAKE2b-256 d6a71f89facc0fb7b0b99dd52f2f861f84c55b5d4bcfc7e56b37d8a971fbc2be

See more details on using hashes here.

File details

Details for the file accrete-0.0.154-py3-none-any.whl.

File metadata

  • Download URL: accrete-0.0.154-py3-none-any.whl
  • Upload date:
  • Size: 1.5 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.3

File hashes

Hashes for accrete-0.0.154-py3-none-any.whl
Algorithm Hash digest
SHA256 aa5bc217be9e74d01fb76ef8d011b646c3f90de5652ebd40c11d9cb7262978a4
MD5 310641e93d69554d68b73386d8988c9b
BLAKE2b-256 2174cd5a42530fce21599d9e50187de5b984c77d0be72abf43e5513b401d0d51

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page