Skip to content

Introduce a serializer field for GenericForeignKeys #17936

@jeremystretch

Description

@jeremystretch

Proposed Changes

Devise and implement a custom serializer field to represent GenericForeignKeys on models. This is currently handled using a read-only SerializerMethodField, which requires a boilerplate method be included on the serializer class. For instance, the code below is from the serializer for circuit terminations:

    @extend_schema_field(serializers.JSONField(allow_null=True))
    def get_termination(self, obj):
        if obj.termination_id is None:
            return None
        serializer = get_serializer_for_model(obj.termination)
        context = {'request': self.context['request']}
        return serializer(obj.termination, nested=True, context=context).data

Justification

Introducing a custom field to represent GFK relationships allows us to enforce a standard implementation and reduce boilerplate.

Metadata

Metadata

Assignees

Labels

netboxstatus: acceptedThis issue has been accepted for implementationtype: housekeepingChanges to the application which do not directly impact the end user
No fields configured for Housekeeping.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions