Django Rest Framework: `get_serializer_class` is called several times, with the wrong request method value

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?

+6
source share
1 answer

The reason you call get_serializer_class several times is because you are using the viewable API. If you test it without using a viewable API, for example by forcing a JSON handler ( ?format=json or an Accept header), you will see that it is called one.

The API viewed for viewing generates forms that are displayed based on the serializer, so get_serializer_class is called once for each form and possible request type.

Thus, although the first request, GET makes sense for the original serializer, which is used to process the response data (in this case, a specific object), the following three are customizable for the viewable API. These are calls that occur in the following order: get_serializer , which you see

method changes override_method with a function that emulates an overridden request method, which usually happens in a POST request that needs another method.

+10
source

Source: https://habr.com/ru/post/987496/


All Articles