You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+52-11Lines changed: 52 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -73,15 +73,15 @@ In a SaaS environment, one size doesn't fit all. Tenants often have unique needs
73
73
74
74
### 5. Other Use-Cases
75
75
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.
77
77
78
78
## Example Implementation
79
79
80
80
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`.
81
81
82
82
> 🟩 Note
83
83
>
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`.
85
85
86
86
### Existing Models
87
87
@@ -116,12 +116,15 @@ class Task(models.Model):
116
116
117
117
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.
118
118
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.
120
122
121
123
```python
122
124
from django.db import models
123
125
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
125
128
126
129
127
130
classTaskPriorityOption(AbstractOption):
@@ -132,7 +135,7 @@ class TaskPriorityOption(AbstractOption):
132
135
"Critical": {"option_type": OptionType.OPTIONAL},
133
136
"High": {"option_type": OptionType.MANDATORY},
134
137
"Medium": {"option_type": OptionType.OPTIONAL},
135
-
"Low": {"option_type": OptionType.MANDATORY},
138
+
"Low": {}, # If no option_type is provided, it defaults to OptionType.MANDATORY
136
139
}
137
140
138
141
classMeta(AbstractOption.Meta):
@@ -198,22 +201,60 @@ class Task(models.Model):
198
201
199
202
### Forms
200
203
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.
202
212
203
213
```python
204
214
from django import forms
205
215
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
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
211
252
212
253
`django-tenant-options` provides management commands for easy maintenance:
213
254
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.
Copy file name to clipboardExpand all lines: docs/optionscookbook.md
+33-2Lines changed: 33 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,8 @@
1
-
# Example Use Cases
1
+
# The Options Cookbook
2
2
3
3
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.
4
4
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.
6
6
7
7
> 🟩 Note
8
8
>
@@ -210,6 +210,37 @@ In general, for industry-standard categories or statuses, we recommend providing
210
210
| Hybrid drive | Tenant-Specific Custom Option |
211
211
| Rock carving storage | Tenant-Specific Custom Option |
Copy file name to clipboardExpand all lines: docs/usage.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -26,4 +26,4 @@ DJANGO_TENANT_OPTIONS = {
26
26
}
27
27
```
28
28
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