I can use different serializers for POST
/ GET
requests as follows:
class CommandViewSet(DynamicModelViewSet):
queryset = Command.objects.all()
serializer_class_post = CommandSerializerPost
serializer_class_get = CommandSerializerGet
permission_classes = (AllowAny,)
def get_serializer_class(self):
if self.request.method == 'POST':
return self.serializer_class_post
elif self.request.method == 'GET':
return self.serializer_class_get
Now I would like to use a different serializer for the request and the reply of a POST
request. How can this be accomplished?
You can override serializer's
to_representation()
method for this:UPD
In above code,
CommandSerializerPost
will always returns the output ofCommandSerializerGet
, irrespective of therequest.method
. So it should be like this if you need to change respnse only forGET
request:You can receive data by MySerializer1 and response to request by MySerializer2