Skip to content

Store feature flags directly on ObjectType model #19924

@jeremystretch

Description

@jeremystretch

NetBox version

v4.3.4

Feature type

Change to existing functionality

Proposed functionality

Convert ObjectType from a proxy model to a concrete model inheriting from Django's native ContentType model, and add boolean fields to indicate the presence or absence of support for all well-known model features.

A draft of the updated model definition is below:

class ObjectType(ContentType):
    feature_bookmarks = models.BooleanField()
    feature_change_logging = models.BooleanField()
    feature_cloning = models.BooleanField()
    feature_contacts = models.BooleanField()
    feature_custom_fields = models.BooleanField()
    # ...

    # Retain the current manager
    objects = ObjectTypeManager()

    class Meta:
        verbose_name = _('object type')
        verbose_name_plural = _('object types')

The ObjectTypeManager will then be updated to query on these feature flags directly, rather than interrogating the in-memory registry to determine which features are supported for a model. The signature for querying ObjectTypes by feature within NetBox core and plugins will remain unchanged, e.g. ObjectType.objects.with_feature('event_rules') will still work as before.

The current, complex logic (below) constructs a query matching against all models listed in the registry as supporting a given feature:

q = Q()
for app_label, models in registry['model_features'][feature].items():
    q |= Q(app_label=app_label, model__in=models)
return self.get_queryset().filter(q)

This new approach simplifies the necessary query substantially:

field_name = f'feature_{feature}'
return self.get_queryset().filter(**{field_name: True})

Use case

Feature support for individual models is currently mapped in the application registry, which is compiled at initialization time and considered immutable. As the registry is maintained in isolation from other processes, this approach prohibits the dynamic creation of modifications of models at runtime in plugins, namely netbox-custom-objects.

Moving these feature flags from the in-memory registry to the database removes this limitation, as any new models or changes to existing models are immediately replicated to peer processes by way of updating the PostgreSQL database. This change also affords a much more performant means of querying models by feature, as we can now issue simple boolean queries and avoid complex queries based on app_label and model (name).

Database changes

  • Convert ObjectType to a concrete model, which will result in the creation of a new core_objecttype table
  • Add a boolean field on this model to represent each supported NetBox feature

External dependencies

N/A

Metadata

Metadata

Assignees

Labels

complexity: mediumRequires a substantial but not unusual amount of effort to implementstatus: acceptedThis issue has been accepted for implementationtype: featureIntroduction of new functionality to the application

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions