[Django]-How to serialize a API response (in JSON) and modify and add fields to it in Django Rest Framework?

2👍

✅

Rather than defining it in the model, you can directly attach these fields in the serializer like this(using SerializerMethodField):

class SearchResultSerializer(serializers.ModelSerializer):
        sentiment_score = serializers.SerializerMethodField()
        sentiment_magnitude = serializers.SerializerMethodField()

       class Meta:
          model = Search
          fields = ('title', 'link', 'snippet', 'description','sentiment_score', 'sentiment_magnitude')

       def get_sentiment_magnitude(self, obj):
            # call external api with search obj which has been stored in your previous call
            return data

       def get_sentiment_score(self, obj):
            # call external api with search obj which has been stored in your previous call
            return data

Update

You can use context from any Generic Views or Viewset to pre-populate data. You can try like this:

class YourViewSet(ViewSet):
     ...
     def get_serializer_context(self):
         context = super(YourViewSet, self).get_serializer_context()
         data = get_it_from_api()
         context['sentiment_score'] = data.get('sentiment_score')
         context['sentiment_magnitude'] = data.get('sentiment_magnitude')
         return context

And use it in serializer like this:

class SearchResultSerializer(serializers.ModelSerializer):
        sentiment_score = serializers.SerializerMethodField()
        sentiment_magnitude = serializers.SerializerMethodField()

       class Meta:
          model = Search
          fields = ('title', 'link', 'snippet', 'description','sentiment_score', 'sentiment_magnitude')

       def get_sentiment_magnitude(self, obj):
            return self.context.get('sentiment_magnitude')

       def get_sentiment_score(self, obj):
            return self.context.get('sentiment_score')

Also, even without using the generic views/viewset, you can still pass extra context like this SearchResultSerializer(instance, context={'sentiment_magnitude': sentiment_magnitude, "sentiment_score": sentiment_score}). Please see the documentation.

3👍

You have two options:

Option 1

You can override to_representation method of serializer. Each serializer has a method called to_representation that will create json response that will be passed to users.
for example:

class SearchResultSerializer(serializers.ModelSerializer):

    def to_representation(self, instance):
        r = super(TodoSerializer, self).to_representation(instance)
        r.update({
        'sentiment_score': 'anything you want here'
        })
        return r

Option 2

Use django rest MethodSerializer fields in your serializer.

class SearchResultSerializer(serializers.ModelSerializer):
    sentiment_magnitude = serializers.SerializerMethodField()

   class Meta:
      model = Search
      fields = '__all__'

   def get_sentiment_magnitude(self, obj):
        sentiment_magnitude = "anything you want here"
        return sentiment_magnitude

Leave a comment