Skip to content Skip to sidebar Skip to footer

Django Use Different Serializer Based On Parameter

I have a APIView that provides my model instances. I want to use different serializers based on url parameters, beacause I want to serve different fields based on parameter. I didn

Solution 1:

You can send a parameter or select the serializer based on the action that is taken.

In your "view.py" file you can use the "get_serializer_class (self)" method to do it.

defget_serializer_class(self):
    if'parameter'in self.request.query_params:
        return ParameterSerializer

    if self.action == "list"or self.action == 'retrieve':
        return ListSerializer
    else:
        return CreateSerializer

Post a Comment for "Django Use Different Serializer Based On Parameter"