Django Rest Framework ViewSets
ViewSets - Django REST framework
In Django REST framework, a ViewSet is a class that defines the actions that can be performed on a particular model or resource. A ViewSet is typically paired with a Router, which maps the HTTP methods and URL patterns to the appropriate ViewSet actions.
Django REST framework provides several built-in ViewSet classes, including:
ViewSet: A base class for all otherViewSetclasses. It provides a basic implementation for handling requests and responses, and can be subclassed to create your ownViewSetclasses.ModelViewSet: AViewSetfor working with a model and its related serializer. It provides the default implementation for the following actions:list,create,retrieve,update, anddestroy.ReadOnlyModelViewSet: AViewSetthat provides read-only actions for a model and its related serializer. It provides the default implementation for the following actions:listandretrieve.GenericViewSet: AViewSetthat allows you to define custom actions by mixing and matching different mixins.
ModelViewSet to create a PersonViewSet that provides the default implementation for the list, create, retrieve, update, and destroy actions:This ViewSet will handle the following HTTP methods and URL patterns:
GET /people/: List allPersonobjects.POST /people/: Create a newPersonobject.GET /people/<pk>/: Retrieve a singlePersonobject identified by its primary key (pk).PUT /people/<pk>/orPATCH /people/<pk>/: Update aPersonobject identified by its primary key.DELETE /people/<pk>/: Delete aPersonobject identified by its primary key.
ViewSet classes provided by Django REST framework:The CustomViewSet is a ViewSet that defines a custom list action. It handles a GET request to the /custom-people/ endpoint and returns a list of all Person objects in the database, serialized using the PersonSerializer class.
The ReadOnlyPersonViewSet is a ReadOnlyModelViewSet that provides the default implementation for the list and retrieve actions. It will handle GET requests to the /read-only-people/ and /read-only-people/<pk>/ endpoints, allowing you to list and retrieve Person objects, but not create, update, or delete them.
ViewSet classes that combine the behavior of multiple built-in ViewSet classes. For example, you might use the ListModelMixin, CreateModelMixin, RetrieveModelMixin, UpdateModelMixin, and DestroyModelMixin to create a ViewSet that provides the default implementation for all CRUD actions.Certainly! Here are a few more points to consider when working with ViewSet classes in Django REST framework:
You can override the default behavior of a
ViewSetby defining custom methods for the different actions. For example, you might want to define a customcreatemethod that performs additional processing or validation on the data being created.You can use
ViewSetclasses with aRouterto automatically generate the URL patterns and wire them up to the appropriate actions. For example, you can use theDefaultRouterto automatically generate the URL patterns for aViewSetand include them in your URLconf.You can use the
@actiondecorator to define custom actions on aViewSet. These actions can be mapped to custom methods or to the built-inViewSetactions (e.g.list,create, etc.).
@action decorator to define a custom action on a ViewSet:ViewSet will handle a GET request to the /people/<pk>/greeting/ endpoint, returning a greeting for the Person object identified by its primary key.
Comments