Utility Reference

This section provides an overview of the tasks, utilities, model functions, and object managers available in django-tenant-users. Use them to enhance and streamline your project.

Tasks

tenant_users.tenants.tasks

provision_tenant(tenant_name, tenant_slug, user_email, *, is_staff=False, is_superuser=True, tenant_type=None, schema_name=None, tenant_extra_data=None)[source]

Creates and initializes a new tenant with specified attributes and default roles.

Parameters:
  • tenant_name (str) – The name of the tenant.

  • tenant_slug (str) – A unique slug for the tenant. It’s used to create the schema_name.

  • user_email (str) – Email address of the tenant’s owner. The user must exist beforehand.

  • is_staff (bool, optional) – If True, the user has staff access. Defaults to False.

  • is_superuser (bool, optional) – If True, the user has all permissions. Defaults to True.

  • tenant_type (str, optional) – Type of the tenant, used with HAS_MULTI_TYPE_TENANTS = True.

  • schema_name (str, optional) – The schema name for the tenant. Defaults to a combination of the slug and a timestamp.

  • tenant_extra_data (dict, optional) – Additional data for the tenant model.

Returns:

The Fully Qualified Domain Name (FQDN) for the newly provisioned tenant.

Return type:

str

Raises:
  • InactiveError – If the user is inactive.

  • ExistsError – If the tenant URL already exists.

  • SchemaError – If the tenant type is not valid.

Utilities

tenant_users.tenants.utils

get_current_tenant()[source]
create_public_tenant(domain_url, owner_email, *, is_superuser=False, is_staff=False, tenant_extra_data=None, **owner_extra)[source]

Creates a public tenant and assigns an owner user.

This function sets up a new public tenant in a multi-tenant Django application. It assigns an owner user to the tenant, with the option to specify additional user and tenant attributes.

Parameters:
  • domain_url (str) – The URL for the public tenant’s domain.

  • owner_email (str) – Email address of the owner user.

  • is_superuser (bool) – If True, the owner has superuser privileges. Defaults to False.

  • is_staff (bool) – If True, the owner has staff access. Defaults to False.

  • tenant_extra_data (dict, optional) – Additional data for the tenant model.

  • **owner_extra – Arbitrary keyword arguments for additional owner user attributes.

Returns:

A tuple containing the tenant object, domain object, and user object.

Return type:

tuple