Simplify the declaration of custom field support in models #5957

Closed
opened 2025-12-29 19:34:54 +01:00 by adam · 2 comments
Owner

Originally created by @jeremystretch on GitHub (Jan 17, 2022).

Proposed Changes

Currently, a NetBox model must meet two conditions for custom fields to function properly:

  1. Inherit from CustomFieldsMixin
  2. Register as having custom fields via the extras_features() decorator

The second step is redundant, given that it should be assumed that any model inheriting from CustomFieldsMixin supports custom fields. However, failing to register via extras_features() excludes the model from the list of available models when creating a custom field, and from other such selections.

We can obviate the need to register the model explicitly by instead connecting a receiver function to Django's class_prepared signal and checking for custom field support on the class.

Justification

Eliminates redundant code and reduces the possibility for human error.

Originally created by @jeremystretch on GitHub (Jan 17, 2022). ### Proposed Changes Currently, a NetBox model must meet two conditions for custom fields to function properly: 1. Inherit from CustomFieldsMixin 2. Register as having custom fields via the `extras_features()` decorator The second step is redundant, given that it should be assumed that any model inheriting from CustomFieldsMixin supports custom fields. However, failing to register via `extras_features()` excludes the model from the list of available models when creating a custom field, and from other such selections. We can obviate the need to register the model explicitly by instead connecting a receiver function to Django's [`class_prepared` signal](https://docs.djangoproject.com/en/4.0/ref/signals/#class-prepared) and checking for custom field support on the class. ### Justification Eliminates redundant code and reduces the possibility for human error.
adam added the type: housekeeping label 2025-12-29 19:34:54 +01:00
adam closed this issue 2025-12-29 19:34:54 +01:00
Author
Owner

@jeremystretch commented on GitHub (Jan 17, 2022):

A similar issue exists with the declaration of support for tags: A model must both inherit from TagsMixin (to receive the appropriate manager) and register via extras_features().

@jeremystretch commented on GitHub (Jan 17, 2022): A similar issue exists with the declaration of support for tags: A model must both inherit from TagsMixin (to receive the appropriate manager) and register via `extras_features()`.
Author
Owner

@jeremystretch commented on GitHub (Jan 28, 2022):

This has been resolved under #8392.

@jeremystretch commented on GitHub (Jan 28, 2022): This has been resolved under #8392.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#5957