1👍
✅
Try this
class MyPrimaryKeyRelatedField(PrimaryKeyRelatedField)
def to_representation(self, value):
if self.pk_field is not None:
return self.pk_field.to_representation(value.pk)
return {"id": value.pk}
Update
class MyModelSerializer(serializers.ModelSerializer)
serializer_related_field = MyPrimaryKeyRelatedField
Then replace PrimaryKeyRelatedField
to MyPrimaryKeyRelatedField
and
serializers.ModelSerializer
to MyModelSerializer
Source:stackexchange.com