mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-16 04:02:52 -06:00
Include custom validation in BaseModel
This commit is contained in:
parent
b7682ca9e8
commit
1a8f144f5c
@ -1,23 +1,51 @@
|
|||||||
# Model Features
|
# 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
|
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:
|
||||||
2. Register the model with NetBox as utilizing the feature
|
|
||||||
|
|
||||||
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
|
```python
|
||||||
# models.py
|
# models.py
|
||||||
from django.db.models import models
|
from netbox.models import BaseModel
|
||||||
from netbox.models.features import TagsMixin
|
|
||||||
|
|
||||||
class MyModel(TagsMixin, models.Model):
|
class MyModel(BaseModel):
|
||||||
foo = models.CharField()
|
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
|
!!! 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.
|
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.CustomFieldsMixin
|
||||||
|
|
||||||
|
::: netbox.models.features.CustomValidationMixin
|
||||||
|
|
||||||
::: netbox.models.features.ExportTemplatesMixin
|
::: netbox.models.features.ExportTemplatesMixin
|
||||||
|
|
||||||
::: netbox.models.features.JobResultsMixin
|
::: netbox.models.features.JobResultsMixin
|
||||||
|
@ -27,6 +27,7 @@ plugins:
|
|||||||
- os.environ.setdefault("DJANGO_SETTINGS_MODULE", "netbox.settings")
|
- os.environ.setdefault("DJANGO_SETTINGS_MODULE", "netbox.settings")
|
||||||
- django.setup()
|
- django.setup()
|
||||||
rendering:
|
rendering:
|
||||||
|
heading_level: 3
|
||||||
show_root_heading: true
|
show_root_heading: true
|
||||||
show_root_full_path: false
|
show_root_full_path: false
|
||||||
show_root_toc_entry: false
|
show_root_toc_entry: false
|
||||||
|
@ -22,6 +22,7 @@ __all__ = (
|
|||||||
class BaseModel(
|
class BaseModel(
|
||||||
CustomFieldsMixin,
|
CustomFieldsMixin,
|
||||||
CustomLinksMixin,
|
CustomLinksMixin,
|
||||||
|
CustomValidationMixin,
|
||||||
ExportTemplatesMixin,
|
ExportTemplatesMixin,
|
||||||
JournalingMixin,
|
JournalingMixin,
|
||||||
TagsMixin,
|
TagsMixin,
|
||||||
@ -53,7 +54,7 @@ class ChangeLoggedModel(ChangeLoggingMixin, CustomValidationMixin, BigIDModel):
|
|||||||
abstract = True
|
abstract = True
|
||||||
|
|
||||||
|
|
||||||
class PrimaryModel(BaseModel, ChangeLoggingMixin, CustomValidationMixin, BigIDModel):
|
class PrimaryModel(BaseModel, ChangeLoggingMixin, BigIDModel):
|
||||||
"""
|
"""
|
||||||
Primary models represent real objects within the infrastructure being modeled.
|
Primary models represent real objects within the infrastructure being modeled.
|
||||||
"""
|
"""
|
||||||
@ -63,7 +64,7 @@ class PrimaryModel(BaseModel, ChangeLoggingMixin, CustomValidationMixin, BigIDMo
|
|||||||
abstract = True
|
abstract = True
|
||||||
|
|
||||||
|
|
||||||
class NestedGroupModel(BaseModel, ChangeLoggingMixin, CustomValidationMixin, BigIDModel, MPTTModel):
|
class NestedGroupModel(BaseModel, ChangeLoggingMixin, BigIDModel, MPTTModel):
|
||||||
"""
|
"""
|
||||||
Base model for objects which are used to form a hierarchy (regions, locations, etc.). These models nest
|
Base model for objects which are used to form a hierarchy (regions, locations, etc.). These models nest
|
||||||
recursively using MPTT. Within each parent, each child instance must have a unique name.
|
recursively using MPTT. Within each parent, each child instance must have a unique name.
|
||||||
@ -105,7 +106,7 @@ class NestedGroupModel(BaseModel, ChangeLoggingMixin, CustomValidationMixin, Big
|
|||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
class OrganizationalModel(BaseModel, ChangeLoggingMixin, CustomValidationMixin, BigIDModel):
|
class OrganizationalModel(BaseModel, ChangeLoggingMixin, BigIDModel):
|
||||||
"""
|
"""
|
||||||
Organizational models are those which are used solely to categorize and qualify other objects, and do not convey
|
Organizational models are those which are used solely to categorize and qualify other objects, and do not convey
|
||||||
any real information about the infrastructure being modeled (for example, functional device roles). Organizational
|
any real information about the infrastructure being modeled (for example, functional device roles). Organizational
|
||||||
|
@ -155,7 +155,7 @@ class CustomLinksMixin(models.Model):
|
|||||||
|
|
||||||
class CustomValidationMixin(models.Model):
|
class CustomValidationMixin(models.Model):
|
||||||
"""
|
"""
|
||||||
Enables user-configured validation rules for built-in models by extending the clean() method.
|
Enables user-configured validation rules for models.
|
||||||
"""
|
"""
|
||||||
class Meta:
|
class Meta:
|
||||||
abstract = True
|
abstract = True
|
||||||
|
Loading…
Reference in New Issue
Block a user