DRF là thư viện trên Django chuyên cho REST API: serializer (validate + parse JSON), viewset (gom logic CRUD), router (sinh URL tự động), authentication/permission/throttling/pagination, và browsable API UI để test.
Không có DRF bạn vẫn có thể trả JsonResponse từ Django view, nhưng phải tự code validate, schema, pagination, content negotiation. DRF gói sẵn mấy convention đó để team chỉ tập trung vào domain.
python
# views.py
class PostViewSet(viewsets.ModelViewSet):
queryset = Post.objects.all()
serializer_class = PostSerializer
permission_classes = [IsAuthenticatedOrReadOnly]
# urls.py
router = DefaultRouter()
router.register('posts', PostViewSet)
urlpatterns = router.urls # GET/POST/PUT/PATCH/DELETE /posts/ và /posts/<id>/DRF mạnh nhất khi API chủ yếu là CRUD trên model.
Với API phức tạp (RPC-style, action không phải CRUD), đừng cố ép vào ModelViewSet — dùng APIView hoặc @api_view đọc rõ hơn nhiều.