1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00
Files
netbox-community-netbox/docs/plugins/development/model-features.md

65 lines
2.0 KiB
Markdown
Raw Normal View History

2022-01-19 16:44:18 -05:00
# Model Features
2022-01-20 10:53:00 -05:00
## Enabling NetBox Features
2022-01-19 16:44:18 -05:00
2022-01-20 10:53:00 -05:00
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:
2022-01-19 16:44:18 -05:00
2022-01-20 10:53:00 -05:00
* Custom fields
* Custom links
* Custom validation
* Export templates
* 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 netbox.models import BaseModel
class MyModel(BaseModel):
foo = models.CharField()
...
```
## 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.)
2022-01-19 16:44:18 -05:00
```python
# models.py
from django.db.models import models
2022-01-20 10:53:00 -05:00
from netbox.models.features import ExportTemplatesMixin, TagsMixin
2022-01-19 16:44:18 -05:00
2022-01-20 10:53:00 -05:00
class MyModel(ExportTemplatesMixin, TagsMixin, models.Model):
2022-01-19 16:44:18 -05:00
foo = models.CharField()
...
```
2022-01-20 10:53:00 -05:00
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
2022-01-19 16:44:18 -05:00
!!! 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.
::: netbox.models.features.CustomLinksMixin
::: netbox.models.features.CustomFieldsMixin
2022-01-20 10:53:00 -05:00
::: netbox.models.features.CustomValidationMixin
2022-01-19 16:44:18 -05:00
::: netbox.models.features.ExportTemplatesMixin
::: netbox.models.features.JournalingMixin
::: netbox.models.features.TagsMixin
::: netbox.models.features.WebhooksMixin