Description
Proposed Changes
Currently, NetBox handles "brief" mode requests (e.g. GET /api/dcim/sites/?brief=true
) by swapping out the model's primary serializer class with its nested one (e.g. NestedSiteSerializer
instead of SiteSerializer
). This nested serializer contains a minimal subset of fields from the primary serializer, and exists in the code base as an entirely separate entity.
The proposal here is to use the primary serializer for each model in both normal and brief modes, by defining the relevant fields under the primary serializer's Meta
class. For example, the representation defined by the current NestedSiteSerializer
class
class NestedSiteSerializer(WritableNestedSerializer):
url = serializers.HyperlinkedIdentityField(view_name='dcim-api:site-detail')
class Meta:
model = models.Site
fields = ['id', 'url', 'display', 'name', 'slug']
would be reproduced on SiteSerializer
as
class SiteSerializer(NetBoxModelSerializer):
...
class Meta:
model = Site
fields = [
'id', 'url', 'display', 'name', 'slug', 'status', 'region', 'group', 'tenant', 'facility', ...
]
brief_fields = ['id', 'url', 'display', 'name', 'slug']
The BriefModeMixin
class would no longer be used to dynamically resolve the serializer based on the presence of the brief
query parameter; the primary serializer will always be used. Instead, it will leverage the new dynamic fields capability introduced in #15087 to pass the pre-defined brief_fields
list to the serializer upon instantiation. This will result in the same rendered data as using the nested serializer.
Justification
This change will enable us to remove nested serializers which exist solely to support brief mode. It does not, however, fully obviate the need for nested serializers, as they are still required in many cases to represent related objects. For example, NestedRegionSerializer
is used to represent the Region to which a Site is assigned. (While it may be possible to further reduce our dependency on discrete nested serializer classes, such work is beyond the scope of this initiative.)
Additionally, this work represents the first step toward ultimately deprecating "brief" mode (which is not an immediate goal of this initiative) in favor of the dynamic field specification functionality introduced in #15087.