Add a consistent display field to all REST API serializers #4605

Closed
opened 2025-12-29 18:38:10 +01:00 by adam · 0 comments
Owner

Originally created by @jeremystretch on GitHub (Feb 27, 2021).

Originally assigned to: @jeremystretch on GitHub.

Environment

  • Python version: 3.7.9
  • NetBox version: 2.10.5

Proposed Functionality

Add a new read-only field (e.g. _repr) to all REST API model serializers to convey the normalized representation of each object. This will always be a string suitable for display to a human user.

I'm open to suggestions regarding the specific name, though I am in favor of adopting the leading underscore to indicate a special-use field, in line with _depth on MPTT model serializers (which is used to effect object nesting in rendered selection lists).

Use Case

Establishing a consistent point of reference for API consumers greatly simplifies the logic needed to represent objects to human users. For example, the DynamicModelChoiceField form field accepts a display_field kwarg on initialization. This tells it which attribute of the model to display for each object in the generated selection list. For example, a Prefix will return its prefix attribute, whereas a DeviceType will return its model. Most models return their name.

By ensuring that every serializer includes this standard field, we obviate the need to specify these attributes.

Database Changes

None

External Dependencies

None

Originally created by @jeremystretch on GitHub (Feb 27, 2021). Originally assigned to: @jeremystretch on GitHub. ### Environment * Python version: 3.7.9 * NetBox version: 2.10.5 ### Proposed Functionality Add a new read-only field (e.g. `_repr`) to all REST API model serializers to convey the normalized representation of each object. This will always be a string suitable for display to a human user. I'm open to suggestions regarding the specific name, though I am in favor of adopting the leading underscore to indicate a special-use field, in line with `_depth` on MPTT model serializers (which is used to effect object nesting in rendered selection lists). ### Use Case Establishing a consistent point of reference for API consumers greatly simplifies the logic needed to represent objects to human users. For example, the DynamicModelChoiceField form field accepts a `display_field` kwarg on initialization. This tells it which attribute of the model to display for each object in the generated selection list. For example, a Prefix will return its `prefix` attribute, whereas a DeviceType will return its `model`. Most models return their `name`. By ensuring that every serializer includes this standard field, we obviate the need to specify these attributes. ### Database Changes None ### External Dependencies None
adam added the status: acceptedtype: feature labels 2025-12-29 18:38:10 +01:00
adam closed this issue 2025-12-29 18:38:10 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#4605