Junior — Middle
Can you list the main base classes used in Django REST Framework?
sobes.tech AI
Answer from AI
The main basic classes in Django REST Framework (DRF) that are frequently used:
- APIView — a base class for creating views, providing full control over request handling logic.
- GenericAPIView — extends APIView, adding support for pagination, filtering, and serialization.
- ViewSet — combines the logic of multiple actions (list, create, retrieve, update, destroy) into a single class.
- ModelViewSet — a subclass of ViewSet that automatically implements CRUD operations for a model.
- Serializer — a class for converting complex data types (e.g., Django models) to JSON and back.
- ModelSerializer — a simplified serializer that is automatically generated based on a model.
Example of using ModelViewSet:
from rest_framework import viewsets
from .models import Product
from .serializers import ProductSerializer
class ProductViewSet(viewsets.ModelViewSet):
queryset = Product.objects.all()
serializer_class = ProductSerializer
These classes allow for quick and convenient creation of APIs with minimal code.