1

class Product_List(viewsets.ModelViewSet):          # >>> List of products & add product to cart
    permission_classes = [IsAuthenticated, ] 

    queryset = Item.objects.all()
    serializer_class = ProductListSerializer

    def post(self, request, *args, **kwargs):  # <<< post is working 
        items = get_object_or_404(Item, id=self.kwargs.get('pk'))
        if items.discount_price < items.price:
            ncart = CartItem.objects.create(user=request.user, product=items)
            ncart.total = ncart.quantity * items.discount_price
            ncart.save()
        else:
            ncart = CartItem.objects.create(user=request.user, product=items)
            ncart.total = ncart.quantity * items.price
            ncart.save()
        data = json.dumps(ncart, default=str, indent=1)
        return Response({'msg': 'Product Added to Cart Successfully'}, status=status.HTTP_200_OK)

Actually I tried post with model-view-set with function perform create but its not working, so I just changed name performcreate > post. this is working any one let me know how it is possible, model viewset allows ModelViewSet get_queryset (list retrieve create perform_create update perform_update destroy)this method right , then why?

Naga Rajan
  • 11
  • 1

1 Answers1

0

Everything is correct just rename your post method with create mentioned below

def create(self, request, *args, **kwargs): # <<< post is working