Description
I find that, particularly when it comes to POST methods, that I often need a different serializer for input than for output. E.g., for a a particular model I may need only two or three input values, but the server will calculate/retrieve/whatever some additional values for fields on the model, and all those values need to get back to the client.
So far, my method has been to override get_serializer_class()
to specify a separate input serializer for the request, and then override create()
to use a different serializer for my output. That pattern works, but it took me some time to figure it out because the docs don't really suggest that option; the assumption that the generic APIViews are built around is that you specify one serializer and that is used for both input and output. I agree this generally works in the common case, but using my method breaks a little bit of the CBV magic. In particular, it can be difficult to troubleshoot if you make a mistake specifying a custom output serializer.
I propose two solutions:
- Build a consistent way to optionally specify different input and output serializers, or
- Add some additional documentation explaining the presumption that one serializer is intended for input and output, and best practices for overriding that in the case that the default behavior doesn't suit your use case.