1、显示当前用户的购物车数据
逻辑:需要用到用户id去购物车数据表进行过滤,返回一个购物车数据列表;
继承类:ListAPIView
属性:序列化器,queryset=Cart,authentication_classes=[ ]
具体代码部分:
class CartAPI(ListAPIView):
queryset = Cart.objects.all()
serializer_class = CartItemSerializer
# 需要登录的认证 保证用户是登录的
authentication_classes = [LoginAuthentication]
# 由于前端需要的是如下字典类型,所以需要重写List# this.carts = data.carts;# this.total_price = data.total_price;
def list(self, request, *args, **kwargs):
queryset = self.filter_queryset(self.get_queryset())
page = self.paginate_queryset(queryset)
if page is not None:
serializer = self.get_serializer(page, many=True)
return self.get_paginated_response(serializer.data)
serializer = self.get_serializer(queryset, many=True)
result = { "cart": serializer.data, "total_price": 0 }
return Response({"data": result})
网友评论