Constants
Configuration Constants module for django-omnitenant
This module provides a centralized management system for all configuration key constants used throughout the django-omnitenant package. It implements a singleton pattern using a _Constants class with cached properties to ensure consistent access to configuration keys across the application.
The module serves as the single source of truth for all configuration setting keys, eliminating hardcoded strings throughout the codebase and promoting maintainability, type safety, and consistency.
Usage
from django_omnitenant.constants import constants
Access a configuration key
tenant_model_key = constants.TENANT_MODEL
Use in Django settings
from django.conf import settings tenant_model = settings.OMNITENANT_CONFIG[constants.TENANT_MODEL]
Design Patterns
- Singleton Pattern: Module-level
constantsinstance ensures single source of truth - Cached Properties: @cached_property provides lazy initialization and caching
- String-Based Keys: Constants return strings used as dictionary keys in OMNITENANT_CONFIG
Performance
- Constants are computed only once per application lifetime
- Thread-safe for multithreaded environments
- Minimal overhead with O(1) lookup on subsequent accesses
_Constants
Private class that manages all configuration key constants for django-omnitenant.
Uses Django's @cached_property decorator to lazily initialize and cache constant
values. This class should not be instantiated directly; use the module-level
constants singleton instance instead.
Key Features
- Lazy Initialization: Constants created only when first accessed
- Caching: Values cached after first access for performance
- Type Hints: All properties explicitly return str type
- Centralized Management: All configuration keys defined in one location
Source code in django_omnitenant/constants.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 | |
TENANT_MODEL()
Configuration key for the custom tenant model class path.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Setting key "TENANT_MODEL" |
Usage
from django.conf import settings from django_omnitenant.constants import constants
tenant_model_path = settings.OMNITENANT_CONFIG[constants.TENANT_MODEL]
e.g., "myapp.Tenant"
Source code in django_omnitenant/constants.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | |
DOMAIN_MODEL()
Configuration key for the custom domain model class path.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Setting key "DOMAIN_MODEL" |
Usage
from django.conf import settings from django_omnitenant.constants import constants
domain_model_path = settings.OMNITENANT_CONFIG[constants.DOMAIN_MODEL]
e.g., "myapp.Domain"
Source code in django_omnitenant/constants.py
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | |
OMNITENANT_CONFIG()
Configuration key for the main omnitenant configuration dictionary.
This is the primary setting key containing all django-omnitenant configuration in Django settings.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Setting key "OMNITENANT_CONFIG" |
Usage
from django.conf import settings from django_omnitenant.constants import constants
omnitenant_config = getattr(settings, constants.OMNITENANT_CONFIG, {})
Source code in django_omnitenant/constants.py
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | |
TENANT_RESOLVER()
Configuration key for the tenant resolver implementation class.
Specifies which resolver class is used to determine the current tenant based on incoming requests (e.g., subdomain resolver, custom domain resolver).
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Setting key "TENANT_RESOLVER" |
Usage
from django.conf import settings from django_omnitenant.constants import constants
resolver_class = settings.OMNITENANT_CONFIG[constants.TENANT_RESOLVER]
e.g., "myapp.resolvers.SubdomainResolver"
Source code in django_omnitenant/constants.py
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | |
PUBLIC_DB_ALIAS()
Configuration key for the public/shared database alias.
Specifies the database alias used for shared data across all tenants that is not isolated per-tenant.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Setting key "PUBLIC_DB_ALIAS" |
Usage
from django.conf import settings from django_omnitenant.constants import constants
public_db = settings.OMNITENANT_CONFIG[constants.PUBLIC_DB_ALIAS]
e.g., "default"
Source code in django_omnitenant/constants.py
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | |
MASTER_DB_ALIAS()
Configuration key for the master database alias.
Specifies the database alias for the master/default database that contains core system data and tenant records.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Setting key "MASTER_DB_ALIAS" |
Usage
from django.conf import settings from django_omnitenant.constants import constants
master_db = settings.OMNITENANT_CONFIG[constants.MASTER_DB_ALIAS]
e.g., "master"
Source code in django_omnitenant/constants.py
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | |
SCHEMA_CONFIG()
Configuration key for schema-based multi-tenancy configuration.
Used for schema-based isolation approach where each tenant has its own database schema (typically with PostgreSQL).
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Setting key "schema_config" |
Note
This constant returns "schema_config" (lowercase) unlike other constants which use UPPERCASE keys.
Source code in django_omnitenant/constants.py
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | |
PUBLIC_TENANT_NAME()
Configuration key for the public tenant identifier.
Specifies the name/identifier of the public tenant which contains shared data accessible to all tenants.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Setting key "PUBLIC_TENANT_NAME" |
Usage
from django.conf import settings from django_omnitenant.constants import constants
public_tenant_name = settings.OMNITENANT_CONFIG[constants.PUBLIC_TENANT_NAME]
e.g., "public"
Source code in django_omnitenant/constants.py
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | |
TEST_TENANT_NAME()
Configuration key for the test tenant identifier.
Specifies the name/identifier of the test tenant which contains data for the testing.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Setting key "TEST_TENANT_NAME" |
Usage
from django.conf import settings from django_omnitenant.constants import constants
test_tenant_name = settings.OMNITENANT_CONFIG[constants.TEST_TENANT_NAME]
e.g., "public"
Source code in django_omnitenant/constants.py
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 | |
MASTER_TENANT_NAME()
Configuration key for the master/default tenant identifier.
Specifies the name/identifier of the master tenant which is typically used for the main administrative or system tenant.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Setting key "MASTER_TENANT_NAME" |
Usage
from django.conf import settings from django_omnitenant.constants import constants
master_tenant_name = settings.OMNITENANT_CONFIG[constants.MASTER_TENANT_NAME]
e.g., "master"
Source code in django_omnitenant/constants.py
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 | |
DEFAULT_SCHEMA_NAME()
Configuration key for the default schema name.
Specifies the default database schema name used for schema-based isolation when no specific schema is set for a tenant.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Setting key "DEFAULT_SCHEMA_NAME" |
Usage
from django.conf import settings from django_omnitenant.constants import constants
default_schema = settings.OMNITENANT_CONFIG[constants.DEFAULT_SCHEMA_NAME]
e.g., "public"
Source code in django_omnitenant/constants.py
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 | |
MASTER_CACHE_ALIAS()
Configuration key for the cache backend alias used for master/global caching.
Specifies which cache backend alias is used for caching data that applies across all tenants (master cache).
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Setting key "DEFAULT_CACHE_ALIAS" |
Usage
from django.conf import settings from django_omnitenant.constants import constants
master_cache = settings.OMNITENANT_CONFIG[constants.MASTER_CACHE_ALIAS]
e.g., "default"
Source code in django_omnitenant/constants.py
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 | |
PUBLIC_HOST()
Configuration key for the public/main hostname.
Specifies the main hostname for the public or primary application domain.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Setting key "PUBLIC_HOST" |
Usage
from django.conf import settings from django_omnitenant.constants import constants
public_host = settings.OMNITENANT_CONFIG[constants.PUBLIC_HOST]
e.g., "example.com"
Source code in django_omnitenant/constants.py
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 | |
PATCHES()
Configuration key for optional patches/customizations to apply.
Specifies a list of optional patches or customizations that should be applied to the core django-omnitenant functionality.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Setting key "PATCHES" |
Usage
from django.conf import settings from django_omnitenant.constants import constants
patches = settings.OMNITENANT_CONFIG.get(constants.PATCHES, [])
e.g., ["cache", "celery"]
Source code in django_omnitenant/constants.py
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 | |