Skip to content

Commit 590fce1

Browse files
committed
Improve documentation about config
1 parent 9d1e862 commit 590fce1

File tree

1 file changed

+123
-13
lines changed

1 file changed

+123
-13
lines changed

docs/usage.md

Lines changed: 123 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ DJANGO_TENANT_OPTIONS = {
4242
}
4343
```
4444

45+
See the [Configuration Guide](https://django-tenant-options.readthedocs.io/en/latest/usage.html#configuration-guide) for more details on available settings.
46+
4547
## Core Concepts
4648

4749
### Option Types
@@ -67,6 +69,10 @@ django-tenant-options provides three types of options:
6769

6870
## Model Configuration
6971

72+
> ⚠️ Warning
73+
>
74+
> The package automatically adds an `objects` Manager and associated QuerySet to each option and selection model. If you define a custom Manager or QuerySet, ensure it inherits from `OptionManager` or `OptionQuerySet`, or your optyions and selections will not work as intended. Also, an additional `unscoped` Manager is available for querying all options, including soft-deleted ones.
75+
7076
### 1. Option Models
7177

7278
Create your Option model by inheriting from `AbstractOption`:
@@ -294,42 +300,146 @@ python manage.py maketriggers --force
294300
python manage.py maketriggers --dry-run --verbose
295301
```
296302

297-
## Advanced Configuration
303+
## Configuration Guide
304+
305+
### Basic Configuration
306+
307+
Django Tenant Options can be configured through your Django settings. Add a `DJANGO_TENANT_OPTIONS` dictionary to your settings:
308+
309+
```python
310+
DJANGO_TENANT_OPTIONS = {
311+
"TENANT_MODEL": "myapp.Tenant",
312+
# ... other settings
313+
}
314+
```
315+
316+
### Base Model Classes
317+
318+
Django Tenant Options allows you to customize the base classes used for models, managers, querysets and field types. This is particularly useful when integrating with packages like django-auto-prefetch or implementing custom behavior across all tenant option models.
319+
320+
#### Configuration Through Settings
321+
322+
Configure base classes globally in your Django settings:
323+
324+
```python
325+
DJANGO_TENANT_OPTIONS = {
326+
# Base class settings
327+
"MODEL_CLASS": "auto_prefetch.Model", # Default: django.db.models.Model
328+
"MANAGER_CLASS": "auto_prefetch.Manager", # Default: django.db.models.Manager
329+
"QUERYSET_CLASS": "auto_prefetch.QuerySet", # Default: django.db.models.QuerySet
330+
"FOREIGNKEY_CLASS": "auto_prefetch.ForeignKey", # Default: django.db.models.ForeignKey
331+
"ONETOONEFIELD_CLASS": "auto_prefetch.OneToOneField", # Default: django.db.models.OneToOneField
332+
}
333+
```
334+
335+
#### Programmatic Configuration
336+
337+
For more granular control, you can configure base classes programmatically:
338+
339+
```python
340+
from django_tenant_options.app_settings import model_config
341+
import auto_prefetch
342+
343+
# Configure base classes
344+
model_config.model_class = auto_prefetch.Model
345+
model_config.manager_class = auto_prefetch.Manager
346+
model_config.queryset_class = auto_prefetch.QuerySet
347+
model_config.foreignkey_class = auto_prefetch.ForeignKey
348+
model_config.onetoonefield_class = auto_prefetch.OneToOneField
349+
```
350+
351+
### Model Configuration
352+
353+
#### Tenant Model Settings
354+
355+
```python
356+
DJANGO_TENANT_OPTIONS = {
357+
# Required: Specify your tenant model
358+
"TENANT_MODEL": "myapp.Tenant", # Default: "django_tenant_options.Tenant"
359+
360+
# What happens when a tenant is deleted
361+
"TENANT_ON_DELETE": "models.CASCADE", # Default: models.CASCADE
362+
363+
# Related name templates for tenant relationships
364+
"TENANT_MODEL_RELATED_NAME": "%(app_label)s_%(class)s_related",
365+
"TENANT_MODEL_RELATED_QUERY_NAME": "%(app_label)s_%(class)ss",
366+
}
367+
```
368+
369+
#### Option Model Settings
298370

299-
### 1. Custom Form Fields
371+
```python
372+
DJANGO_TENANT_OPTIONS = {
373+
# What happens when an option is deleted
374+
"OPTION_ON_DELETE": "models.CASCADE", # Default: models.CASCADE
300375

301-
You can customize how options are displayed in forms:
376+
# Related name templates for option relationships
377+
"OPTION_MODEL_RELATED_NAME": "%(app_label)s_%(class)s_related",
378+
"OPTION_MODEL_RELATED_QUERY_NAME": "%(app_label)s_%(class)ss",
379+
380+
# Templates for the many-to-many relationship between options and tenants
381+
"ASSOCIATED_TENANTS_RELATED_NAME": "%(app_label)s_%(class)s_selections",
382+
"ASSOCIATED_TENANTS_RELATED_QUERY_NAME": "%(app_label)s_%(class)ss_selected",
383+
}
384+
```
385+
386+
#### Database Configuration
387+
388+
```python
389+
DJANGO_TENANT_OPTIONS = {
390+
# Override database vendor detection
391+
"DB_VENDOR_OVERRIDE": "postgresql", # Options: 'postgresql', 'mysql', 'sqlite', 'oracle'
392+
}
393+
```
394+
395+
This setting is useful when using custom database backends (e.g., PostGIS) while the underlying database is a supported vendor.
396+
397+
### Form Configuration
398+
399+
```python
400+
DJANGO_TENANT_OPTIONS = {
401+
# Default form field for multiple choice fields
402+
"DEFAULT_MULTIPLE_CHOICE_FIELD": "myapp.CustomOptionsField", # Default: OptionsModelMultipleChoiceField
403+
404+
# Control behavior of deleted selections in forms
405+
"DISABLE_FIELD_FOR_DELETED_SELECTION": False, # Default: False
406+
}
407+
```
408+
409+
When `DISABLE_FIELD_FOR_DELETED_SELECTION` is True, deleted selections appear disabled in forms rather than requiring selection of a new option.
410+
411+
## Extending Models and Queries
412+
413+
### Custom Form Fields
414+
415+
You can customize how options are displayed in forms by subclassing `OptionsModelMultipleChoiceField`:
302416

303417
```python
304418
from django_tenant_options.form_fields import OptionsModelMultipleChoiceField
305419

306420
class CustomOptionsField(OptionsModelMultipleChoiceField):
307421
def label_from_instance(self, obj):
308422
return f"{obj.name} - {obj.get_option_type_display()}"
309-
310-
DJANGO_TENANT_OPTIONS = {
311-
"DEFAULT_MULTIPLE_CHOICE_FIELD": CustomOptionsField,
312-
}
313423
```
314424

315-
### 2. Custom Managers and QuerySets
425+
### Extending QuerySets and Managers
316426

317-
Extend the default managers and querysets for additional functionality:
427+
Add custom functionality by subclassing the base querysets and managers:
318428

319429
```python
320430
from django_tenant_options.models import OptionQuerySet, OptionManager
321431

322432
class CustomOptionQuerySet(OptionQuerySet):
323-
def active_mandatory(self):
324-
return self.active().filter(option_type="dm")
433+
def active_priority(self):
434+
return self.active().filter(priority__gt=0)
325435

326436
class CustomOptionManager(OptionManager):
327437
def get_queryset(self):
328438
return CustomOptionQuerySet(self.model, using=self._db)
329439

330-
class TaskPriorityOption(AbstractOption):
440+
class PriorityOption(AbstractOption):
331441
objects = CustomOptionManager()
332-
# ... rest of the model definition
442+
priority = models.IntegerField(default=0)
333443
```
334444

335445
### Performance Optimization

0 commit comments

Comments
 (0)