Skip to content

Commit 2c1a930

Browse files
committed
Update docs
1 parent ccc222b commit 2c1a930

4 files changed

Lines changed: 87 additions & 15 deletions

File tree

README.md

Lines changed: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,15 @@ In a SaaS environment, one size doesn't fit all. Tenants often have unique needs
7373

7474
### 5. Other Use-Cases
7575

76-
See additional [use cases](https://django-tenant-options.readthedocs.io/en/latest/usecases/) in the documentation for more inspiration.
76+
See [the options cookbook](https://django-tenant-options.readthedocs.io/en/latest/optionscookbook.html) in the documentation for more inspiration.
7777

7878
## Example Implementation
7979

8080
Consider a scenario where your SaaS provides project management tools for businesses. Each User is associated with a Tenant, and can create Tasks. You want each tenant to be able to customize the available task "priorities" and "status" provided in user-facing task tracking forms. Here’s how you can implement this with `django-tenant-options`.
8181

8282
> 🟩 Note
8383
>
84-
> See the [example project](./exampleproject.md) for the more detailed demonstration of how to set up a multi-tenant application with custom options using `django-tenant-options`.
84+
> See the [example project](https://django-tenant-options.readthedocs.io/en/latest/exampleproject.html) for the more detailed demonstration of how to set up a multi-tenant application with custom options using `django-tenant-options`.
8585
8686
### Existing Models
8787

@@ -116,12 +116,15 @@ class Task(models.Model):
116116

117117
Each Set of options in `django-tenant-options` is defined by two models: an `Option` model, which stores all mandatory, optional, and custom options, and a `Selection` model, which identifies which options are currently associated with a tenant.
118118

119-
With a `Tenant` model and a `Task` model in your project, you can implement the `TaskPriorityOption` and `TaskPrioritySelection` models as follows:
119+
With a `Tenant` model and a `Task` model in your project, you can implement the `TaskPriorityOption` and `TaskPrioritySelection` models, which inherit from [`AbstractOption`](https://django-tenant-options.readthedocs.io/en/latest/reference.html#django_tenant_options.models.AbstractOption) and [`AbstractSelection`](https://django-tenant-options.readthedocs.io/en/latest/reference.html#django_tenant_options.models.AbstractSelection) respectively.
120+
121+
In each Option model, you can define the `default_options` dictionary for task priorities or status, including which options are mandatory and which are optional. In this example, "High" and "Low" priorities are mandatory for all tenants (and users will always see these options in forms), while "Critical" and "Medium" priorities are optional for selection by tenants. Tenants can also create custom priorities as needed.
120122

121123
```python
122124
from django.db import models
123125

124-
from django_tenant_options.models import AbstractOption, AbstractSelection, OptionType
126+
from django_tenant_options.models import AbstractOption, AbstractSelection
127+
from django_tenant_options.choices import OptionType
125128

126129

127130
class TaskPriorityOption(AbstractOption):
@@ -132,7 +135,7 @@ class TaskPriorityOption(AbstractOption):
132135
"Critical": {"option_type": OptionType.OPTIONAL},
133136
"High": {"option_type": OptionType.MANDATORY},
134137
"Medium": {"option_type": OptionType.OPTIONAL},
135-
"Low": {"option_type": OptionType.MANDATORY},
138+
"Low": {}, # If no option_type is provided, it defaults to OptionType.MANDATORY
136139
}
137140

138141
class Meta(AbstractOption.Meta):
@@ -198,22 +201,60 @@ class Task(models.Model):
198201

199202
### Forms
200203

201-
`django-tenant-options` provides a set of forms to manage the options and selections for each tenant. You can use these forms in your views to allow tenants to customize their options.
204+
`django-tenant-options` provides a set of form mixins and fields to manage the options and selections for each tenant. You can use these forms in your views to allow tenants to customize their options.
205+
206+
- `OptionCreateFormMixin` and `OptionUpdateFormMixin` are provided to create and update Options.
207+
- `SelectionForm` is used to manage the Selections associated with a tenant.
208+
- `UserFacingFormMixin` is provided to ensures ForeignKey fields to an `AbstractOption` subclass are populated with the correct tenant Selections.
209+
- `OptionsModelMultipleChoiceField` is a customized `ModelMultipleChoiceField` that retrieves the Options and also displays the option type associated with each Option.
210+
211+
You are encouraged to extend these Mixins and Fields to suit your project's needs.
202212

203213
```python
204214
from django import forms
205215

206-
from django_tenant_options.forms import OptionForm, SelectionForm
216+
from django_tenant_options.forms import OptionCreateFormMixin, OptionUpdateFormMixin, SelectionForm, UserFacingFormMixin
217+
from example.models import Task, TaskPriorityOption, TaskPrioritySelection
218+
219+
220+
class TaskForm(UserFacingFormMixin, forms.ModelForm):
221+
"""Form for creating and updating a Task."""
222+
class Meta:
223+
model = Task
224+
fields = "__all__"
225+
207226

227+
class TaskPriorityOptionCreateForm(OptionCreateFormMixin, forms.ModelForm):
228+
"""Form for creating a TaskPriorityOption."""
229+
class Meta:
230+
model = TaskPriorityOption
231+
fields = "__all__"
232+
233+
234+
class TaskPriorityOptionUpdateForm(OptionUpdateFormMixin, forms.ModelForm):
235+
"""Form for updating a TaskPriorityOption."""
236+
class Meta:
237+
model = TaskPriorityOption
238+
fields = "__all__"
239+
240+
241+
class TaskPrioritySelectionForm(SelectionForm):
242+
"""Form for selecting TaskPriorityOptions."""
243+
class Meta:
244+
model = TaskPrioritySelection
208245
```
209246

210-
### Management Commands
247+
### Views, Templates, and URLs
248+
249+
Views, Templates, and URLs can be implemented as needed to allow tenants to manage their options. Views tfor forms that use `OptionCreateFormMixin`, `OptionUpdateFormMixin`, and `SelectionForm` must pass the tenant instance to the form's `tenant` attribute.
250+
251+
## Management Commands
211252

212253
`django-tenant-options` provides management commands for easy maintenance:
213254

214-
- **`listoptions`**: Lists all available options in the database.
215-
- **`syncoptions`**: Synchronizes the `defaults` in each model with the database when a change in the model has been made. Should always be run after any migrations have been completed.
216-
- **`maketriggers`**: Creates database trigger migrations to ensure there can never be mismatch between a Tenant and an associated Option.
255+
- **[`listoptions`](https://django-tenant-options.readthedocs.io/en/latest/reference.html#listoptions)**: Lists all available options in the database.
256+
- **[`syncoptions`](https://django-tenant-options.readthedocs.io/en/latest/reference.html#syncoptions)**: Synchronizes the `default_options` in each model with the database when a change in the model has been made. Should always be run after any migrations have been completed.
257+
- **[`maketriggers`](https://django-tenant-options.readthedocs.io/en/latest/reference.html#maketriggers-options)**: Creates database trigger migrations to ensure there can never be mismatch between a Tenant and an associated Option.
217258

218259
```bash
219260
python manage.py syncoptions

docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ usage
1414
terminology
1515
exampleproject
1616
reference
17-
usecases
17+
optionscookbook
1818
contributing
1919
Code of Conduct <codeofconduct>
2020
License <license>
Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
# Example Use Cases
1+
# The Options Cookbook
22

33
To help you get started, we have compiled a list of common data categories and their recommended types. These categories can be used as a reference when setting up your options in a multi-tenant application.
44

5-
In general, for industry-standard categories or statuses, we recommend providing options as mandatory (always displayed in a tenant's user-facing form) or optional (each tenant can choose whether it is displayed to users as an option they can select). For categories or statuses that are specific to a tenant's business or industry, they can create custom options to meet their unique needs.
5+
In general, for industry-standard categories or statuses, we recommend providing options as mandatory (always displayed in a tenant's user-facing form) or optional (each tenant can choose whether it is displayed to users as an option they can select). For categories or statuses that are specific to a tenant's business or organization, they can create custom options to meet their unique needs.
66

77
> 🟩 Note
88
>
@@ -210,6 +210,37 @@ In general, for industry-standard categories or statuses, we recommend providing
210210
| Hybrid drive | Tenant-Specific Custom Option |
211211
| Rock carving storage | Tenant-Specific Custom Option |
212212

213+
## Infrastructure - Road Types
214+
215+
| Option | Recommended Type |
216+
|-------------------|--------------------------------|
217+
| Highway | Mandatory |
218+
| Street | Mandatory |
219+
| Avenue | Mandatory |
220+
| Lane | Mandatory |
221+
| Boulevard | Optional |
222+
| Alley | Optional |
223+
| Freeway | Optional |
224+
| Expressway | Optional |
225+
| Parkway | Optional |
226+
| Bike path | Tenant-Specific Custom Option |
227+
228+
## Insurance - Policy Types
229+
230+
| Option | Recommended Type |
231+
|---------------------------|--------------------------------|
232+
| Life | Mandatory |
233+
| Health | Mandatory |
234+
| Auto | Mandatory |
235+
| Homeowners | Mandatory |
236+
| Renters | Mandatory |
237+
| Disability | Optional |
238+
| Long-term care | Optional |
239+
| Travel | Optional |
240+
| Pet | Optional |
241+
| Business | Optional |
242+
| Alien abduction | Tenant-Specific Custom Option |
243+
213244
## Learning Management System (LMS) - Course Categories & Grading Schemes
214245

215246
| Option | Recommended Type |

docs/usage.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ DJANGO_TENANT_OPTIONS = {
2626
}
2727
```
2828

29-
For more information on the available settings, see the [`app_settings.py` reference](./reference.md#module-django_tenant_options.app_settings).
29+
For more information on the available settings, see the [`app_settings.py` reference](https://django-tenant-options.readthedocs.io/en/latest/reference.html#module-django_tenant_options.app_settings).

0 commit comments

Comments
 (0)