1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

Include custom validation in BaseModel

This commit is contained in:
jeremystretch
2022-01-20 10:53:00 -05:00
parent b7682ca9e8
commit 1a8f144f5c
4 changed files with 44 additions and 12 deletions

View File

@@ -1,23 +1,51 @@
# Model Features
Plugin models can leverage certain NetBox features by inheriting from designated Python classes (documented below), defined in `netbox.models.features`. These classes perform two crucial functions:
## Enabling NetBox Features
1. Apply any fields, methods, or attributes necessary to the operation of the feature
2. Register the model with NetBox as utilizing the feature
Plugin models can leverage certain NetBox features by inheriting from NetBox's `BaseModel` class. This class extends the plugin model to enable numerous feature, including:
For example, to enable support for tags in a plugin model, it should inherit from `TagsMixin`:
* Custom fields
* Custom links
* Custom validation
* Export templates
* Job results
* Journaling
* Tags
* Webhooks
This class performs two crucial functions:
1. Apply any fields, methods, or attributes necessary to the operation of these features
2. Register the model with NetBox as utilizing these feature
Simply subclass BaseModel when defining a model in your plugin:
```python
# models.py
from django.db.models import models
from netbox.models.features import TagsMixin
from netbox.models import BaseModel
class MyModel(TagsMixin, models.Model):
class MyModel(BaseModel):
foo = models.CharField()
...
```
This will ensure that TaggableManager is applied to the model, and that the model is registered with NetBox as taggable.
## Enabling Features Individually
If you prefer instead to enable only a subset of these features for a plugin model, NetBox provides a discrete "mix-in" class for each feature. You can subclass each of these individually when defining your model. (You will also need to inherit from Django's built-in `Model` class.)
```python
# models.py
from django.db.models import models
from netbox.models.features import ExportTemplatesMixin, TagsMixin
class MyModel(ExportTemplatesMixin, TagsMixin, models.Model):
foo = models.CharField()
...
```
The example above will enable export templates and tags, but no other NetBox features. A complete list of available feature mixins is included below. (Inheriting all the available mixins is essentially the same as subclassing `BaseModel`.)
## Feature Mixins Reference
!!! note
Please note that only the classes which appear in this documentation are currently supported. Although other classes may be present within the `features` module, they are not yet supported for use by plugins.
@@ -26,6 +54,8 @@ This will ensure that TaggableManager is applied to the model, and that the mode
::: netbox.models.features.CustomFieldsMixin
::: netbox.models.features.CustomValidationMixin
::: netbox.models.features.ExportTemplatesMixin
::: netbox.models.features.JobResultsMixin