Django REST Framework Views - Generic Views
Class based views - Django Rest Framework
Django REST framework provides a set of generic views that you can use to quickly build API views. These views are designed to provide common behavior, such as allowing read-only or authenticated access to API endpoints.
Django's generic views... were developed as a shortcut for common usage patterns... They take certain common idioms and patterns found in view development and abstract them so that you can quickly write common views of data without having to repeat yourself.
Here is a list of the generic views provided by Django REST framework:
APIView: A base class for all other generic views. It provides the core implementation for handling requests and responses, and can be subclassed to create your own views.CreateAPIView: A view for creating a new instance of a model. It handles the POST request and provides a default implementation for saving the new object to the database.ListAPIView: A view for listing a queryset of objects. It handles the GET request and provides a default implementation for pagination and filtering.RetrieveAPIView: A view for retrieving a single instance of a model. It handles the GET request and provides a default implementation for looking up the object by its primary key.UpdateAPIView: A view for updating an instance of a model. It handles the PUT or PATCH request and provides a default implementation for saving the updated object to the database.DestroyAPIView: A view for deleting an instance of a model. It handles the DELETE request and provides a default implementation for deleting the object from the database.
The PersonListView will handle a GET request to the /people/ endpoint and return a list of all Person objects in the database.
The PersonCreateView will handle a POST request to the /people/ endpoint and create a new Person object in the database.
The PersonDetailView will handle a GET request to the /people/<pk>/ endpoint and return a single Person object, identified by its primary key (pk).
The PersonUpdateView will handle a PUT or PATCH request to the /people/<pk>/ endpoint and update the Person object identified by its primary key.
The PersonDestroyView will handle a DELETE request to the /people/<pk>/ endpoint and delete the Person object identified by its primary key.
Certainly! In addition to the generic views provided by Django REST framework, you can also create your own custom views by subclassing APIView and implementing the methods for handling different HTTP request methods (e.g. get, post, put, etc.).
Here is an example of a custom view that handles both GET and POST requests:
from rest_framework.response import Response from rest_framework.views import APIView from .models import Person from .serializers import PersonSerializer class CustomPersonView(APIView): def get(self, request): people = Person.objects.all() serializer = PersonSerializer(people, many=True) return Response(serializer.data) def post(self, request): serializer = PersonSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def put(self, request, pk): person = self.get_object(pk) serializer = PersonSerializer(person, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
This view will handle a GET request to the /custom-people/ endpoint and return a list of all Person objects in the database, serialized using the PersonSerializer class. It will also handle a POST request to the same endpoint, creating a new Person object in the database with the data provided in the request.
The put method uses the PersonSerializer to validate and save the updated data to the database. If the serializer is valid, it returns the serialized data in the response. If the serializer is invalid, it returns an error response with a status code of 400 Bad Request.
Comments