[Answered ]-DRF- how to return field which is not included in model in response

1👍

You can simply call the serializer’s method.

class SomeView(APIView):
   def get(self, request):
       serializer = serializers.SomeViewSerializer()
       return Response({'result_url': serializer.get_url()})

0👍

Try this one. Add obj parameter into MethodFiled get_url(self, obj)

class SomeViewSerializer(serializers.Serializer):
   url = serializers.SerializerMethodField()

   def get_url(self, obj):
      res_id = genetare_url(self.id)
      return res_id

and pass any data into serializer

ex: SomeViewSerializer(request.data) like this:

class SomeView(APIView):

    def get(self, request, format=None):
        serializer = SomeViewSerializer(request.data)
        return Response(serializer.data)

This might help you.

Leave a comment