Enable plugins to register custom model features #11500

Closed
opened 2025-12-29 21:46:03 +01:00 by adam · 0 comments
Owner

Originally created by @jeremystretch on GitHub (Aug 19, 2025).

Originally assigned to: @jeremystretch on GitHub.

NetBox version

v4.4.0-beta1

Feature type

New functionality

Proposed functionality

NetBox support various model features such as tags, custom fields, change logging, etc. which enhance the application's functionality on a per-model basis. These features are enabled for models by inheriting a designated mixin class: For example, tagging is enabled for all models which inherit the netbox.models.features.TagsMixin class. These feature-to-class mappings are defined statically in the FEATURES_MAP constant:

FEATURES_MAP = {
    'bookmarks': BookmarksMixin,
    'change_logging': ChangeLoggingMixin,
    'cloning': CloningMixin,
    ...
}

This static mapping can be replaced with a dynamic registration system which can be employed by both core NetBox apps and plugins to register model features. For example, the tagging feature mentioned above would be registered like this:

@register_model_feature('tags')
def support_tagging(model):
    return issubclass(model, TagsMixin)

This approach allows for complex logic within the litmus function bound to the declared feature, however in many cases it could be simplified to a lambda invoking issubclass() directly:

register_model_feature('tags', lambda model: issubclass(model, TagsMixin))

The result is that registry['model_features'] will now hold a mapping of feature names to evaluation functions, rather than to mixin classes.

Use case

This approach provides two benefits over the current implementation:

  1. Plugins can introduce new features and apply them to core models.
  2. The logic for determining whether a feature is supported for a given model can be extended beyond simple inheritance checking.

For example, the netbox-branching plugin would register branching as a model feature, using a function it provides to determine eligibility on a per-model basis.

While this has previously been possible by modifying the model_features registry directly, FR #19924 in NetBox v4.4 now records model features on ObjectType records in the database, so registry hacking is no longer feasible.

Database changes

N/A

External dependencies

N/A

Originally created by @jeremystretch on GitHub (Aug 19, 2025). Originally assigned to: @jeremystretch on GitHub. ### NetBox version v4.4.0-beta1 ### Feature type New functionality ### Proposed functionality NetBox support various _model features_ such as tags, custom fields, change logging, etc. which enhance the application's functionality on a per-model basis. These features are enabled for models by inheriting a designated mixin class: For example, tagging is enabled for all models which inherit the `netbox.models.features.TagsMixin` class. These feature-to-class mappings are defined statically in the [`FEATURES_MAP`](https://github.com/netbox-community/netbox/blob/9580ac2946e4b702e249c37aaf9ec81a85229e19/netbox/netbox/models/features.py#L618) constant: ```python FEATURES_MAP = { 'bookmarks': BookmarksMixin, 'change_logging': ChangeLoggingMixin, 'cloning': CloningMixin, ... } ``` This static mapping can be replaced with a dynamic registration system which can be employed by both core NetBox apps and plugins to register model features. For example, the tagging feature mentioned above would be registered like this: ```python @register_model_feature('tags') def support_tagging(model): return issubclass(model, TagsMixin) ``` This approach allows for complex logic within the litmus function bound to the declared feature, however in many cases it could be simplified to a lambda invoking `issubclass()` directly: ```python register_model_feature('tags', lambda model: issubclass(model, TagsMixin)) ``` The result is that `registry['model_features']` will now hold a mapping of feature names to evaluation functions, rather than to mixin classes. ### Use case This approach provides two benefits over the current implementation: 1. Plugins can introduce new features and apply them to core models. 2. The logic for determining whether a feature is supported for a given model can be extended beyond simple inheritance checking. For example, the [netbox-branching plugin](https://github.com/netboxlabs/netbox-branching) would register `branching` as a model feature, using a function it provides to determine eligibility on a per-model basis. While this has previously been possible by modifying the `model_features` registry directly, FR #19924 in NetBox v4.4 now records model features on ObjectType records in the database, so registry hacking is no longer feasible. ### Database changes N/A ### External dependencies N/A
adam added the status: acceptedtype: featurecomplexity: medium labels 2025-12-29 21:46:03 +01:00
adam closed this issue 2025-12-29 21:46:04 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#11500