Understanding CRUD operations with Django views and the request methods

When the Django server receives an HTTP request, Django creates an HttpRequest instance, specifically a django.http.HttpRequest object. This instance contains metadata about the request, and this metadata includes an HTTP verb such as GET, POST, or PUT. The method attribute provides a string representing the HTTP verb or method used in the request.

When Django loads the appropriate view that will process the request, it passes the HttpRequest instance as the first argument to the view function. The view function has to return an HttpResponse instance, specifically a django.http.HttpResponse instance.

The toy_list function lists all the toys or creates a new toy. The function receives an HttpRequest instance in the request argument. The function is capable of processing two HTTP verbs: GET and POST. The code checks the value of the request.method attribute to determine the code to be executed based on the HTTP verb.

If the HTTP verb is GET, the expression request.method == 'GET' will evaluate to True and the code has to list all the toys. The code will retrieve all the Toy objects from the database, use the ToySerializer to serialize all of them and return a JSONResponse instance built with the data generated by the ToySerializer serializer. The code creates the ToySerializer instance with the many=True argument to specify that multiple instances have to be serialized and not just one. Under the hood, Django uses a ListSerializer instance when the many argument value is set to True. This way, Django is capable of serializing a list of objects.

If the HTTP verb is POST, the code has to create a new toy based on the JSON data that is included in the body of the HTTP request. First, the code uses a JSONParser instance and calls its parse method with the request parameter that the toy_list function receives as an argument to parse the toy data provided as JSON data in the request body and saves the results in the toy_data local variable. Then, the code creates a ToySerializer instance with the previously retrieved data and calls the is_valid method to determine whether the Toy instance is valid or not. If the instance is valid, the code calls the save method to persist the instance in the database and returns a JSONResponse with the saved data in its body and a status equal to status.HTTP_201_CREATED, that is, 201 Created.

Whenever we have to return a specific status different from the default 200 OK status, it is a good practice to use the module variables defined in the rest_framework.status module and avoid using hard-coded numeric values. If you see status=status.HTTP_201_CREATED, as in the sample code, it is easy to understand that the status is an HTTP 201 Created status. If you read status=201, you have to remember what the number 201 stands for in the HTTP status codes.

The toy_detail function retrieves, updates, or deletes an existing toy. The function receives an HttpRequest instance in the request argument and the identifier for the toy to be retrieved, updated, or deleted in the pk argument. The function is capable of processing three HTTP verbs: GET, PUT, and DELETE. The code checks the value of the request.method attribute to determine the code to be executed based on the HTTP verb.

No matter what the HTTP verb is, the toy_detail function calls the Toy.objects.get method with the received pk as the pk argument to retrieve a Toy instance from the database based on the specified identifier, and saves it in the toy local variable. In case a toy with the specified identifier doesn't exist in the database, the code returns an HttpResponse with its status set to status.HTTP_404_NOT_FOUND, that is, 404 Not Found.

If the HTTP verb is GET, the code creates a ToySerializer instance with toy as an argument and returns the data for the serialized toy in a JSONResponse that will include the default HTTP 200 OK status. The code returns the retrieved toy serialized as JSON in the response body.

If the HTTP verb is PUT, the code has to create a new toy based on the JSON data that is included in the HTTP request and use it to replace an existing toy. First, the code uses a JSONParser instance and calls its parse method with request as an argument to parse the toy data provided as JSON data in the request and saves the results in the toy_data local variable. Then, the code creates a ToySerializer instance with the Toy instance previously retrieved from the database (toy) and the retrieved data that will replace the existing data (toy_data). Then, the code calls the is_valid method to determine whether the Toy instance is valid or not. If the instance is valid, the code calls the save method to persist the instance with the replaced values in the database and returns a JSONResponse with the saved data serialized as JSON in its body and the default HTTP 200 OK status. If the parsed data doesn't generate a valid Toy instance, the code returns a JSONResponse with a status equal to status.HTTP_400_BAD_REQUEST, that is 400 Bad Request.

If the HTTP verb is DELETE, the code calls the delete method for the Toy instance previously retrieved from the database (toy). The call to the delete method erases the underlying row in the toys_toy table that we analyzed in the previous chapter. Thus, the toy won't be available anymore. Then, the code returns a JSONResponse with a status equal to status. HTTP_204_NO_CONTENT that is, 204 No Content.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.14.1.136