Junior — Middle
What is the main purpose of ViewSet in the Django REST Framework?
sobes.tech AI
Answer from AI
A ViewSet in Django REST Framework is a class that combines the logic for handling HTTP requests for a set of related actions with a model (for example, create, read, update, delete). The main goal of a ViewSet is to simplify API development by providing ready-made methods for standard operations and enabling quick creation of RESTful interfaces.
Example of using a ViewSet:
from rest_framework import viewsets
from myapp.models import Product
from myapp.serializers import ProductSerializer
class ProductViewSet(viewsets.ModelViewSet):
queryset = Product.objects.all()
serializer_class = ProductSerializer
This ViewSet automatically supports GET, POST, PUT, DELETE operations for the Product model.