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.
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:Justification
Introducing a custom field to represent GFK relationships allows us to enforce a standard implementation and reduce boilerplate.