How can modify response send by django rest framework
angularjs, django, django-rest-framework, python, rest
Solution
You can use the transform_field method which allows you to customize the representation of a field when showing it.
Documentation about it: http://www.django-rest-framework.org/api-guide/serializers/#customizing-field-representation
Btw, careful if you are planning to upgrade to new versions of drf (>=3), these methods may disappear in favour of a unique `to_representation` method: https://groups.google.com/forum/?fromgroups#!topic/django-rest-framework/9kp5kXCssR4
Problem
I am using `Django REST as backend` and angular as front end. I have two serializers one for readong GET requests and other for POST, PUT requests. This was because there are few fields like `interval`, etc which i am entering in integer by user but in the database i am saving as timedelta so i have to multitply them to make them as seconds on front end. so e.g `interval = 5` entered by user and i am posting `5*60*60` to server. In order to read i have made `ReadSerializer` where i am diving that by 60*60 to again show to user what he added. This is working find my problem is after saving my object to database the djnago rest frameework sends the object as it is saved which has `interval = 5*60*60`. inerval is just an example there 4-5 felds where i am changing them in front end before posting Is there any way that response used my `READ serializer before sending` ``` class Serializer(serializers.ModelSerializer): interval = serializers.SerializerMethodField('get_interval') class Meta: model = User fields = ('id', 'interval') def get_interval(self, obj): return obj.interval/60*60 class WriteSerializer(serializers.ModelSerializer): class Meta: model = User fields = ('id', 'interval') ``` This is the view ``` class UserListCreateView(generics.ListCreateAPIView): queryset = User.objects.filter(is_deleted=False) def get_serializer_class(self): if self.request.method == 'GET': return ReadSerializer return WriteSerializer ```