Todo:
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):
...
They are class that encapsulates some logics
from rest_framework.mixins import ListModelMizin, CreateModelMizin
They are concrete class that combines multiple mixins
from rest_framework.generics import ListCreateAPIView