Interview questions related to viewset
viewset.Modelviewset can operate all crud operations?
Yes, viewsets.ModelViewSet in Django REST Framework (DRF) can operate all CRUD (Create, Read, Update, Delete) operations. It is a combination of multiple view classes provided by DRF that makes it easy to implement standard CRUD operations.
Here is a summary of the operations supported by viewsets.ModelViewSet:
- Create: Allows the creation of new records.
- Read (Retrieve): Allows retrieval of a single record or a list of records.
- Update: Allows updating existing records (both full update and partial update).
- Delete: Allows deletion of existing records.
ModelViewSet, you need to define a serializer class and a queryset in your viewset. Here is an example:In your urls.py, you can register this viewset with a router to automatically generate the URL patterns for the CRUD operations:
from rest_framework.routers import DefaultRouter from .views import YourModelViewSet router = DefaultRouter() router.register(r'yourmodel', YourModelViewSet) urlpatterns = router.urlsThis setup will provide the following endpoints:
GET /yourmodel/: List all records (Read)POST /yourmodel/: Create a new record (Create)GET /yourmodel/{pk}/: Retrieve a specific record (Read)PUT /yourmodel/{pk}/: Update a specific record (Update - full update)PATCH /yourmodel/{pk}/: Update a specific record (Update - partial update)DELETE /yourmodel/{pk}/: Delete a specific record (Delete)This makes
ModelViewSeta powerful and convenient tool for implementing CRUD functionality in your Django REST Framework project.
what about simple viewset?
A simple
viewsetin Django REST Framework (DRF) does not provide all the CRUD operations out of the box likeModelViewSetdoes. Instead, it provides more flexibility, allowing you to define which actions you want to support and how they should be implemented. There are different types of viewsets, such asViewSet,GenericViewSet, andReadOnlyModelViewSet, each providing different levels of functionality.
ViewSet: The base class for all viewsets. You need to define the actions (
create,retrieve,update,partial_update,destroy, andlist) yourself.GenericViewSet: This extends
ViewSetand adds generic views (mixins) for common actions. You can combine it with mixins likeCreateModelMixin,RetrieveModelMixin,UpdateModelMixin,DestroyModelMixin, andListModelMixinto achieve the desired functionality.ReadOnlyModelViewSet: This is a special case of
GenericViewSetthat only provides read-only operations (listandretrieve).Using
ViewSetIf you use the base
ViewSet, you need to manually define each action. Here's an example:
from rest_framework import viewsets from rest_framework.response import Response from .models import YourModel from .serializers import YourModelSerializer class YourModelViewSet(viewsets.ViewSet): def list(self, request): queryset = YourModel.objects.all() serializer = YourModelSerializer(queryset, many=True) return Response(serializer.data) def retrieve(self, request, pk=None): queryset = YourModel.objects.all() model = get_object_or_404(queryset, pk=pk) serializer = YourModelSerializer(model) return Response(serializer.data) def create(self, request): serializer = YourModelSerializer(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 update(self, request, pk=None): model = get_object_or_404(YourModel, pk=pk) serializer = YourModelSerializer(model, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) def partial_update(self, request, pk=None): model = get_object_or_404(YourModel, pk=pk) serializer = YourModelSerializer(model, data=request.data, partial=True) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) def destroy(self, request, pk=None): model = get_object_or_404(YourModel, pk=pk) model.delete() return Response(status=status.HTTP_204_NO_CONTENT)
Using
GenericViewSetwith MixinsFor more convenience, you can use
GenericViewSetwith the appropriate mixins:
from rest_framework import viewsets, mixins from .models import YourModel from .serializers import YourModelSerializer class YourModelViewSet(mixins.CreateModelMixin, mixins.RetrieveModelMixin, mixins.UpdateModelMixin, mixins.DestroyModelMixin, mixins.ListModelMixin, viewsets.GenericViewSet): queryset = YourModel.objects.all() serializer_class = YourModelSerializer
Using
ReadOnlyModelViewSetIf you only need read-only operations:
from rest_framework import viewsets from .models import YourModel from .serializers import YourModelSerializer class YourModelViewSet(viewsets.ReadOnlyModelViewSet): queryset = YourModel.objects.all() serializer_class = YourModelSerializer
Registering ViewSets with Routers
In your
urls.py, you register your viewsets with a router, just like withModelViewSet:
from rest_framework.routers import DefaultRouter from .views import YourModelViewSet router = DefaultRouter() router.register(r'yourmodel', YourModelViewSet) urlpatterns = router.urls
look in viewset.ViewSet this ViewSet is mixin?The
viewsets.ViewSetin Django REST Framework (DRF) is not a mixin itself. Instead, it is a base class that provides the basic structure for implementing your own viewsets. It does not provide any default implementations for the common CRUD actions. You need to define these actions yourself when you useviewsets.ViewSet.Here's a breakdown of how
viewsets.ViewSetworks:
viewsets.ViewSetThe
viewsets.ViewSetclass provides the basic methods for handling requests (get_object,get_queryset,get_serializer, etc.), but you need to define the actual CRUD operations likelist,retrieve,create,update,partial_update, anddestroy.Here is an example of how you can implement a
ViewSetwith all CRUD operations:
from rest_framework import viewsets, status from rest_framework.response import Response from rest_framework.decorators import action from django.shortcuts import get_object_or_404 from .models import YourModel from .serializers import YourModelSerializer class YourModelViewSet(viewsets.ViewSet): def list(self, request): queryset = YourModel.objects.all() serializer = YourModelSerializer(queryset, many=True) return Response(serializer.data) def retrieve(self, request, pk=None): queryset = YourModel.objects.all() model = get_object_or_404(queryset, pk=pk) serializer = YourModelSerializer(model) return Response(serializer.data) def create(self, request): serializer = YourModelSerializer(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 update(self, request, pk=None): model = get_object_or_404(YourModel, pk=pk) serializer = YourModelSerializer(model, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) def partial_update(self, request, pk=None): model = get_object_or_404(YourModel, pk=pk) serializer = YourModelSerializer(model, data=request.data, partial=True) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) def destroy(self, request, pk=None): model = get_object_or_404(YourModel, pk=pk) model.delete() return Response(status=status.HTTP_204_NO_CONTENT)
Registering the ViewSet with a Router
In your
urls.py, you register thisViewSetwith a router:from rest_framework.routers import DefaultRouter from .views import YourModelViewSet router = DefaultRouter() router.register(r'yourmodel', YourModelViewSet, basename='yourmodel') urlpatterns = router.urlsUsing Mixins with
GenericViewSetIf you prefer to use mixins to provide default implementations for the CRUD operations, you can use
GenericViewSetcombined with the appropriate mixins. This approach reduces boilerplate code.Here's an example using
GenericViewSetwith mixins:
from rest_framework import mixins, viewsets from .models import YourModel from .serializers import YourModelSerializer class YourModelViewSet(mixins.CreateModelMixin, mixins.RetrieveModelMixin, mixins.UpdateModelMixin, mixins.DestroyModelMixin, mixins.ListModelMixin, viewsets.GenericViewSet): queryset = YourModel.objects.all() serializer_class = YourModelSerializer
Summary
- viewsets.ViewSet: A base class for viewsets. You need to define all CRUD operations (
list,retrieve,create,update,partial_update,destroy) manually.- Mixins with
GenericViewSet: Provides default implementations for CRUD operations, reducing boilerplate code. You can use mixins likeCreateModelMixin,RetrieveModelMixin,UpdateModelMixin,DestroyModelMixin, andListModelMixin.By using
viewsets.ViewSet, you get the flexibility to define custom behavior for each action. Using mixins withGenericViewSetis more convenient when you want to quickly implement standard CRUD operations.

Post a Comment