Todo:

  1. Class-Based views
  2. Generic Views
  3. Mixins

Class Based Views

from rest_framework.views import APIView
# This is the base class for all class-based views

class ProductList(APIView):
	
	def get(self,request):
		queryset = Product.objects.select_related("collection").all()
		serializer = ProductSerializer(queryset, many=True, context={'request':request}
		return Response(serializer.data)
	
	def post(self,request):
		serializer = ProductSerializer(data=request.data)
		serializer.is_valid(raise_exception=True)
		serializer.save()
		return Response(serializer.data)

now, change the URLs

path("products/", views.ProductList.as_view(), name="products")

Single Entity

class ProductDetail(APIView):

	def get(self, request, id):
		...

Mixins

They are class that encapsulates some logics

from rest_framework.mixins import ListModelMizin, CreateModelMizin

Generic Views:

They are concrete class that combines multiple mixins

from rest_framework.generics import ListCreateAPIView