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, owner, *, 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.
owner (UserModel) – The owner (User) of the provision tenant.
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:
- A tuple containing:
tenant object: The provisioned tenant instance created.
domain object: The Fully Qualified Domain Name (FQDN) instance for the newly provisioned tenant.
- Return type:
tuple
- 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]
Retrieves the current tenant based on the current database schema.
This function gets the current schema name from the database connection, retrieves the tenant model, and then fetches the tenant instance that matches the current schema name.
- Returns:
The tenant instance corresponding to the current database schema.
- Return type:
tenant
- create_public_tenant(domain_url, owner_email, *, is_superuser=False, is_staff=False, tenant_extra_data=None, verbosity=1, **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.
verbosity (int, optional) – Verbosity level for saving the tenant. Defaults to 1.
**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