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)
实际上,我尝试与模型视图集与功能执行创建,但它不工作,所以我只是改变了名称performcreate>职位。这是可行的,有人告诉我这是怎么可能的,模型视图集允许ModelViewSetget_queryset(列表检索创建perform_create更新perform_update摧毁)这个方法,那为什么呢?
一切都是正确的,只是用下面提到的create重命名你的post方法
defcreate (self, request, *args, **kwargs): # <<