mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Add documentation on 'validate_model_deletion' (#7280)
* Add documentation on 'validate_model_deletion' * Update code inclusion * Set type annotation * docstring updates * Cleanup * Hide summary * Remove duplicated code
This commit is contained in:
parent
1328c3d103
commit
c1def12203
docs/docs/extend/plugins
src/backend/InvenTree/plugin/base/integration
@ -14,29 +14,35 @@ Any of the methods described below can be implemented in a custom plugin to prov
|
||||
!!! info "Multi Plugin Support"
|
||||
It is possible to have multiple plugins loaded simultaneously which support validation methods. For example when validating a field, if one plugin returns a null value (`None`) then the *next* plugin (if available) will be queried.
|
||||
|
||||
## Model Deletion
|
||||
|
||||
Any model which inherits the `PluginValidationMixin` class is exposed to the plugin system for custom deletion validation. Before the model is deleted from the database, it is first passed to the plugin ecosystem to check if it really should be deleted.
|
||||
|
||||
A custom plugin may implement the `validate_model_deletion` method to perform custom validation on the model instance before it is deleted.
|
||||
|
||||
::: plugin.base.integration.ValidationMixin.ValidationMixin.validate_model_deletion
|
||||
options:
|
||||
show_bases: False
|
||||
show_root_heading: False
|
||||
show_root_toc_entry: False
|
||||
show_sources: True
|
||||
summary: False
|
||||
members: []
|
||||
|
||||
## Model Validation
|
||||
|
||||
Any model which inherits the `PluginValidationMixin` mixin class is exposed to the plugin system for custom validation. Before the model is saved to the database (either when created, or updated), it is first passed to the plugin ecosystem for validation.
|
||||
|
||||
Any plugin which inherits the `ValidationMixin` can implement the `validate_model_instance` method, and run a custom validation routine.
|
||||
|
||||
The `validate_model_instance` method is passed the following arguments:
|
||||
|
||||
| Argument | Description |
|
||||
| --- | --- |
|
||||
| `instance` | The model instance to be validated |
|
||||
| `deltas` | A dict of field deltas (if the instance is being updated) |
|
||||
|
||||
```python
|
||||
def validate_model_instance(self, instance, deltas=None):
|
||||
"""Validate the supplied model instance.
|
||||
|
||||
Arguments:
|
||||
instance: The model instance to be validated
|
||||
deltas: A dict of field deltas (if the instance is being updated)
|
||||
"""
|
||||
...
|
||||
```
|
||||
::: plugin.base.integration.ValidationMixin.ValidationMixin.validate_model_instance
|
||||
options:
|
||||
show_bases: False
|
||||
show_root_heading: False
|
||||
show_root_toc_entry: False
|
||||
show_sources: True
|
||||
summary: False
|
||||
members: []
|
||||
|
||||
### Error Messages
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
"""Validation mixin class definition."""
|
||||
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.db.models import Model
|
||||
|
||||
import part.models
|
||||
import stock.models
|
||||
@ -49,7 +50,7 @@ class ValidationMixin:
|
||||
"""Raise a ValidationError with the given message."""
|
||||
raise ValidationError(message)
|
||||
|
||||
def validate_model_deletion(self, instance):
|
||||
def validate_model_deletion(self, instance: Model) -> None:
|
||||
"""Run custom validation when a model instance is being deleted.
|
||||
|
||||
This method is called when a model instance is being deleted.
|
||||
@ -59,14 +60,14 @@ class ValidationMixin:
|
||||
instance: The model instance to validate
|
||||
|
||||
Returns:
|
||||
None or True (refer to class docstring)
|
||||
None: or True (refer to class docstring)
|
||||
|
||||
Raises:
|
||||
ValidationError if the instance cannot be deleted
|
||||
ValidationError: if the instance cannot be deleted
|
||||
"""
|
||||
return None
|
||||
|
||||
def validate_model_instance(self, instance, deltas=None):
|
||||
def validate_model_instance(self, instance: Model, deltas: dict = None) -> None:
|
||||
"""Run custom validation on a database model instance.
|
||||
|
||||
This method is called when a model instance is being validated.
|
||||
@ -77,10 +78,10 @@ class ValidationMixin:
|
||||
deltas: A dictionary of field names and updated values (if the instance is being updated)
|
||||
|
||||
Returns:
|
||||
None or True (refer to class docstring)
|
||||
None: or True (refer to class docstring)
|
||||
|
||||
Raises:
|
||||
ValidationError if the instance is invalid
|
||||
ValidationError: if the instance is invalid
|
||||
"""
|
||||
return None
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user