Using decorators to enable different parsers and renderers

We will make changes to just one file. After you save the changes, Django's development server will automatically restart. However, you can decide to stop Django's development server and start it again after you finish all the necessary changes.

We will make the necessary changes to use the previously introduced @api_view decorator to make it possible for the RESTful Web Service to work with different parsers and renderers, by taking advantage of generalized behaviors provided by the APIView class.

Now, go to the restful01/toys folder and open the views.py file. Replace the code in this file with the following lines. However, take into account that many lines have been removed, such as the lines that declared the JSONResponse class. The code file for the sample is included in the hillar_django_restful_04_02 folder, in the restful01/toys/views.py file:

from django.shortcuts import render 
from rest_framework import status 
from toys.models import Toy 
from toys.serializers import ToySerializer
from rest_framework.decorators import api_view 
from rest_framework.response import Response 
 
@api_view(['GET', 'POST']) 
def toy_list(request): 
    if request.method == 'GET': 
        toys = Toy.objects.all() 
        toys_serializer = ToySerializer(toys, many=True) 
        return Response(toys_serializer.data) 
 
    elif request.method == 'POST': 
        toy_serializer = ToySerializer(data=request.data) 
        if toy_serializer.is_valid(): 
            toy_serializer.save() 
            return Response(toy_serializer.data, status=status.HTTP_201_CREATED) 
        return Response(toy_serializer.errors, status=status.HTTP_400_BAD_REQUEST) 
 
@api_view(['GET', 'PUT', 'DELETE']) 
def toy_detail(request, pk): 
    try: 
        toy = Toy.objects.get(pk=pk) 
    except Toy.DoesNotExist: 
        return Response(status=status.HTTP_404_NOT_FOUND) 
 
    if request.method == 'GET': 
        toy_serializer = ToySerializer(toy) 
        return Response(toy_serializer.data) 
 
    elif request.method == 'PUT': 
        toy_serializer = ToySerializer(toy, data=request.data) 
        if toy_serializer.is_valid(): 
            toy_serializer.save() 
            return Response(toy_serializer.data) 
        return Response(toy_serializer.errors, status=status.HTTP_400_BAD_REQUEST) 
 
    elif request.method == 'DELETE': 
        toy.delete() 
        return Response(status=status.HTTP_204_NO_CONTENT) 

The new code applies the @api_view decorator for the two functions: toy_list and toy_detail. In addition, the new code removes the JSONResponse class and uses the more generic rest_framework.response.Response class.

We had to remove the usage of the rest_framework.parsers.JSONParser class in the functions to make it possible to work with different parsers. This way, we stopped working with a parser that only works with JSON. In the older version of the code, the toy_list function executed the following two lines when the request.method attribute was equal to 'POST':

toy_data = JSONParser().parse(request) 
toy_serializer = ToySerializer(data=toy_data) 

In the new code, we removed the first line that called the JSONParser().parse method that was only capable of parsing JSON content. The new code replaces the two previous lines with the following single line that passes request.data as the data argument to create a new ToySerializer instance:

toy_serializer = ToySerializer(data=request.data) 

In the older version of the code, the toy_detail function executed the following two lines when the request.method attribute was equal to 'PUT':

toy_data = JSONParser().parse(request) 
toy_serializer = ToySerializer(toy, data=toy_data)

We made edits that are similar to the changes done for the code in the toy_list function. We removed the first line that called the JSONParser().parse method that was only capable of parsing JSON content. The new code replaces the two previous lines with the following single line that passes toy as the first argument and request.data as the data argument to create a new ToySerializer instance:

toy_serializer = ToySerializer(toy, data=request.data) 
..................Content has been hidden....................

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