![]() delete () return Response ( status = status. HTTP_400_BAD_REQUEST ) def delete ( self, request, pk, format = None ): snippet = self. get_object ( pk ) serializer = SnippetSerializer ( snippet, data = request. data ) def put ( self, request, pk, format = None ): snippet = self. get_object ( pk ) serializer = SnippetSerializer ( snippet ) return Response ( serializer. DoesNotExist : raise Http404 def get ( self, request, pk, format = None ): snippet = self. """ def get_object ( self, pk ): try : return Snippet. Retrieve, update or delete a snippet instance. The APIView classįrom snippets.models import Snippet from rializers import SnippetSerializer from django.http import Http404 from rest_framework.views import APIView from rest_framework.response import Response from rest_framework import status class SnippetDetail ( APIView ): """ It also provides some conveniences like returning 405 Method Not Allowed responses when appropriate, and handling any ParseError exception that occurs when accessing request.data with malformed input. ![]() ![]() Note: The decorator ensures that the view receives an instance of the django rest framework’s Request object (instead of the standard Django HttpRequest object) and returns an instance of the django rest framework’s Response object (instead of the standard Django HttpResponse object). We select the object from the database and then just use an if block to check the request’s method and proceed accordingly. Nothing fancy here, the logic flow is straightforward. method = 'PUT' : serializer = SnippetSerializer ( snippet, data = request. method = 'GET' : serializer = SnippetSerializer ( snippet ) return Response ( serializer. DoesNotExist : return Response ( status = status. Retrieve, update or delete a code snippet. Function based viewsįrom rest_framework import status from rest_corators import api_view from rest_framework.response import Response from snippets.models import Snippet from rializers import SnippetSerializer api_view () def snippet_detail ( request, pk ): """ The object’s detail view is responsible for retrieving, updating or deleting the object or in other words handling the GET, PUT and DELETE method requests to the detail view’s url. In this case the object is a Code Snippet as used in the django rest tutorial but you can imagine the object being anything you want, for example a Post or a Comment. Our example will focus on the simple detail view of an object. In the rest of this post we will show how we can achieve the same result with each one of the four different view styles. By the way, a very useful place to start familiarizing yourself with django class based views is the Classy Class-Based Views and the Classy Django REST Framework for Django REST framework class based views respectively. This doesn’t mean that you can’t customize a class based view to any degree of complexity, but someone must be familiar with how class based views work behind the scenes, for example which methods are called in what order, in order to fully follow the view’s logic. Class based views on the other hand give you the possibility to achieve a typical behaviour without much code, almost with no code at all, as will become obvious in a moment. In a nutshell we can say that there are four ways to write a django rest view, beginning from the standard function based view and moving to different styles of class based views.įunction based views are usually chosen when we want to highly customize the typical view logic since it is easier to follow the sequence of actions they describe. But no matter if you are fan of them or not, you have to admit that they offer you the possibility to do so many things with just a few lines of code. As most things in life class based views come with their own pros and cons and their adoption is a matter of preference. ![]() ![]() Similarly to django, django rest framework offers you the choice of using class based views. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |