Using ModelViewSet , is it normal for get_serializer_class call several times for a single request when accessing the API? And what self.method.request value of self.method.request change between each call?
I created a small test project to show the behavior . In project/example/views.py there is a ThingViewSet with a custom get_serializer_class that prints the current request method.
If you start the server and go to http://127.0.0.1:8000/things/1/ , the result will be something like this:
./manage.py runserver Performing system checks... System check identified no issues (0 silenced). May 19, 2015 - 08:51:34 Django version 1.8.1, using settings 'project.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CONTROL-C. Method is: GET Method is: PUT Method is: PATCH Method is: PUT [19/May/2015 08:51:40]"GET /things/1/ HTTP/1.1" 200 11679
It is clear that get_serializer_class is called 4 times with different values ( GET , PUT , PATCH , PUT ), although only one GET request is executed.
The strange thing is that this does not happen if you request it as JSON:
./manage.py runserver Performing system checks... System check identified no issues (0 silenced). May 19, 2015 - 10:25:57 Django version 1.8.1, using settings 'project.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CONTROL-C. Method is: GET [19/May/2015 10:26:22]"GET /things/?format=json HTTP/1.1" 200 49
And the problem is that the request method in the last call to get_serializer_class API being viewed for viewing is PUT (which is clearly wrong for the GET request), and then we end up using the wrong serializer for the request, given that for different request methods the different serializers that we execute in our real-life project are returned (for example, for read and write operations).
Can someone shed light on what is happening? Why is get_serializer_class called multiple times for an API browser with incorrect method values?