Skip to main content

Multi Tenant App for Django

Shared schema multi tenant system for Django.

Setup

pip install accrete

  • Add 'accrete' to INSTALLED_APPS

    # settings.py
    
    INSTALLED_APPS = [
        ...
        'accrete',
        'django...',
    ]
    
  • Add accrete.middleware.TenantMiddleware after the auth middleware

    # settings.py
    
    MIDDLEWARE = [
        ...
        'django.contrib.auth.middleware.AuthenticationMiddleware',
        'accrete.middleware.TenantMiddleware',
        ...
    ]
    
  • Run migrations

Overview

Tenants and their Members are separated by a ForeignKey to accrete.models.Tenant and authenticated by request parameters and headers. On Models inheriting from accrete.modeles.TenantModel and Forms/ModelForms inheriting from accrete.form.(Model)Form, querysets are filtered automatically. The authenticated tenant and member are stored in a contextvar and can be retrieved and set from anywhere in the code.

Models

accrete.models.TenantModel

Abstract Base Model that links to accrete.models.Tenant.
Sets the objects attribute to accrete.managers.TenantManager.
Adds safety and integrity checks to the save method.

Inherit form this model to enable tenant separation.

# models.py

from accrete.models import TenantModel

class MyModel(TenantModel):
    ...

accrete.models.Tenant

Model representing a tenant. The active tenant for a request/process is stored in a contexvar and can be retrieved and set by accrete.tenant.get_tenant and accrete.tenant.set_tenant respectively.

accrete.models.Member

This model links the user model to the tenant model, allowing users to be associated with multiple tenants. The member, like the tenant, is authenticated by the accrete.middleware.TenantMiddleware and stored in a contextvar.
The active member can be retived by accrete.tenant.get_mamber and set by accrete.tenant.set_member. Using set_member also sets the appropriate tenant.

accret.models.AccessGroup

Arbitrary authorization groups that can be set on tenant or members.
These groups are checked on views decorated by accrete.decorator.tenant_reuired or subclassed from accrete.mixins.TenantRequiredMixin.

Manager

accrete.managers.TenantManager

Filters QuerySets by the active Tenant.
Ensures the active tenant is set on the instances on bulk operations.

Middleware

tenant.middleware.TenantMiddleware

This Middleware adds the Tenant(request.tenant) and Member(request.member) attributes to the request object.
On responses the X-TENANT-ID header and tenant_id url parameter is added with the active Tenant-ID as the value.

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

  • The "tenant_id" URL Parameter in the GET data
  • The Header X-TENANT-ID

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

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

Views

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.

tenant.decorator.tenant_required

Substitute for django.contrib.auth.decorators.login_required
Checks if a tenant is set on the request and redirects to the TENANT_NOT_SET_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

accrete.forms.Form

Form class that filters the queryset of every field with a queryset attribute.

accrete.forms.ModelForm

Same behavior as tenant.forms.Form. Additionally, sets the tenant on the instance on save if needed.

Fields

accrete.fields.TranslatedCharField

Extends JsonField to store strings in different languages with the language code as the key.
By default, the language specified in settings.LANGUAGE_CODE is used to store values.

from django.db import models
from django.utils import translation
from accrete.utils.models import translated_db_value


class MyModel(models.Model):
    name = TranslatedCharField(...)

    
# Create instance with the language set in settings.LANGUAGE_CODE
instance = MyModel(name='Name in default language')
instance.save()

# Switch to german
translation.activate('de-de')
instance.name = 'Name auf Deutsch'
instance.save()

# Switch back to english
translation.activate('en-us')
instance.refresh_from_db()
print(instance.name)
>>> 'Name in default language'

# Utility to get the saved json as a dict
print(translated_db_value(instance, 'name'))
>>> {'en-us': 'Name in default language', 'de-de': 'Name auf Deutsch'}

# Create an instance with multiple translations at once
translation.activate('de-de')
instance = MyModel(name={'en-us': 'Name in default language', 'de-de': 'Name auf Deutsch'})
instance.save()
print(instance.name)
>>> 'Name auf Deutsch'

Channels

accrete.channels.TenantMiddleware

Support for django-channels. Sets the tenant and member contextvar.
Must be inside auth middleware.

# asgi.py

application = ProtocolTypeRouter({
    "http": django_asgi_app,
    "websocket": AllowedHostsOriginValidator(
        AuthMiddlewareStack(
            TenantMiddleware(
                URLRouter([
                    path('path/to/consumer/', Consumer.as_asgi()),
                ])
            )
        )
    ),
})

Consumer

accrete.consumer.WebsocketTenantConsumer

Checks on connect if a tenant is set.

accrete.consumer.JsonWebsocketTenantConsumer

Checks on connect if a tenant is set.

Settings

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

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

Tenant Utils

Tenant

accrete.tenant.set_tenant(tenant: accrete.models.Tenant)

Stores the tenant in contextvar.

accrete.tenant.get_tenant

Retrieves the tenant from contextvar.

accrete.tenant.set_member(member: accrete.models.Member)

Stores the member and associated tenant contextvar.

accrete.tenant.set_member(member: accrete.models.Member)

Retrieves the member from contextvar.

accrete.tenant.unscoped()

Context Manager to temporally disable tenant isolation.

accrete.tenant.unscope()

Decorator to disable tenant isolation.

accrete.tenant.per_tenant(include: Q = None, exclude: Q = None)

Decorator to run the decorated function per tenant. Tenant to run on can be filtered by the include or exclude arguments.

Admin

Tenants are supported in the admin inferface with the apps
accrete.contrib.admin and accrete.contrib.admin_templates.

  • Add admin and admin_templates apps to INSTALLED_APPS
    admin_template must be before admin for the template loader to find the templates
    INSTALLED_APPS = [
      ...
      'accrete.contrib.admin_templates',
      'accrete.contrib.admin',
      ...
  ]
  • Add 'accrete.contrib.admin.middleware.TenantMiddleware' to the MIDDLEWARE
    setting after 'accrete.middleware.TenantMiddleware'
  • Subclass your tenant aware Models from accrete.contrib.admin.admin.TenantModelAdmin
    instead of the dfault ModelAdmin

Contrib

Additional Apps

App Description
admin AdminSite and ModelAdmin classes for tenant support in admin
admin_templates Extension of admin templates
country Countries with translatable names
log Global auditlog, runs at post_save signal
sequence Configurable sequences with support for subsequences per year.
system_mail Celery mail queue
ui User Interface based on HTMX, Alpine.js and Bulma
user Custom User model
user_registration User registration using system_mail to send confirmation mails

Metadata

Release files for accrete 0.4.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for accrete 0.4.1
File Size Uploaded Provenance Actions
accrete-0.4.1.tar.gz 1.5 MB Without provenance Details for accrete-0.4.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for accrete 0.4.1
File Interpreter ABI Platform Actions
accrete-0.4.1-py3-none-any.whl Python 3 none any Details for accrete-0.4.1-py3-none-any.whl

Total release size: 3.1 MB

Release files / accrete-0.4.1.tar.gz

Download URL accrete-0.4.1.tar.gz
Size 1.5 MB
Tags Source
SHA-256 checksum
How to use checksums
6131bc48fa47f2765d3774628139f0a8c9107550e00c373ae97e6598310e5415
BLAKE2b-256 checksum
How to use checksums
6bce079a7db9d3a4923c9b420f978cef6f641b03eafd48d85ca7d2c2927b210a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Release files / accrete-0.4.1-py3-none-any.whl

Download URL accrete-0.4.1-py3-none-any.whl
Size 1.6 MB
Tags Python 3
SHA-256 checksum
How to use checksums
bf2e1a322bc38f32c3ed402bfbfa3a1fc06f0d63bf74e746860d2375b72c417f
BLAKE2b-256 checksum
How to use checksums
13a6e152812a1d9b6aa531bdec93b37abd83293ebe47b7967eda95cace57fde7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Security for accrete 0.4.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.4.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page