def perform_create(self, serializer):
        order = serializer.save()  # 创建一笔订单
        global order_sn
        order_sn += 1
        order.order_sn = '000000' + str(order_sn)

        order.save()
        print('----->', serializer.validated_data)
        # 清空购物车
        '''
        goods_id   user_id  nums
          1         5        2   ----->shopcart
          6         5        5
          9         5        1
          [shopcart,shopcart,shopcart]
        '''
        shopcarts = ShoppingCart.objects.filter(user=self.request.user)
        for shopcart in shopcarts:
            orderGoods = OrderGoods()
            orderGoods.goods_num = shopcart.nums
            orderGoods.goods = shopcart.goods
            orderGoods.order = order
            orderGoods.save()

            # delete
            shopcart.delete()
    def perform_create(self, serializer):
        # 保存后会返回OrderInfo订单的实例对象
        order_info = serializer.save()

        # 查找当前用户下购物车所有的商品
        shopping_carts = ShoppingCart.objects.filter(
            user=self.request.user)  # 列表

        for shopping_cart in shopping_carts:
            # 订单详情商品
            order_goods = OrderGoods()

            # 订单
            order_goods.order = order_info

            # 先要保存到OrderGoods,取出购物车的商品
            goods = shopping_cart.goods
            # 保存到订单对应的商品model
            order_goods.goods = goods

            # 订单商品详情的数量
            order_goods.goods_num = shopping_cart.nums

            # 保存OrderGoods
            order_goods.save()

            # 把购物车里面的所有的商品删除,在删除之前,
            shopping_cart.delete()
Exemplo n.º 3
0
 def perform_create(self, serializer):
     order = serializer.save()
     shop_carts = ShoppingCart.objects.filter(user=self.request.user)
     for shop_cart in shop_carts:
         order_goods = OrderGoods()
         order_goods.goods = shop_cart.goods
         order_goods.goods_num = shop_cart.nums
         order_goods.order = order
         order_goods.save()
         shop_cart.delete()
     return order
Exemplo n.º 4
0
 def perform_create(self, serializer):
     order = serializer.save() # 保存OrderInfo(订单)并返回
     # 1.从购物车里的数据生成订单
     shop_carts = ShoppingCart.objects.filter(user=self.request.user)
     for shop_cart in shop_carts:
         order_goods = OrderGoods()
         order_goods.goods = shop_cart.goods
         order_goods.goods_num = shop_cart.nums
         order_goods.order = order # 删除的时候是级联删除
         order_goods.save()
         # 2.清空购物车
         shop_cart.delete()
     return order
Exemplo n.º 5
0
    def post(self, request):
        """ post提交订单 """

        # 接受参数
        user = request.user
        address_id = request.POST.get('params[address_id]')
        address = UserAddress.objects.get(id=address_id, user=user)
        pay_method = request.POST.get('params[pay_method]')
        goods_id = request.POST.getlist('params[goods_id][]')
        post_message = request.POST.get('params[post_message]', '')
        nums = int(request.POST.get('params[nums]', '1'))

        # 参数检验
        if not all([address_id, pay_method, goods_id]):
            return HttpResponse('{"status": "fail", "msg": "参数有错或购物车为空"}', content_type='application/json')

        # 事务保存点
        sid = transaction.savepoint()

        order_info = OrderInfo()
        order_info.user = user
        order_info.signer = address.signer
        order_info.signer_mobile = address.signer_mobile
        order_info.address = address.address

        order_info.order_sn = datetime.now().strftime("%Y%m%d%H%M%S") + str(user.id)
        order_info.pay_method = pay_method
        order_info.post_message = post_message
        order_info.save()

        order_mount = 0  # 总价格
        for good_id in goods_id:
            try:  # 购物车里创建的订单
                cart_good = ShoppingCart.objects.get(goods_id=good_id)
                cart_good_nums = cart_good.nums
            except:  # 这个是 用户立即购买创建的订单
                cart_good_nums = nums

            good = Goods.objects.get(id=good_id)
            if cart_good_nums > good.goods_num:
                # 回滚事务
                transaction.savepoint_rollback(sid)
                return HttpResponse('{"status": "fail", "msg": "商品库存不足"}', content_type='application/json')
            else:
                order_good = OrderGoods()
                order_good.goods = good
                order_good.goods_num = cart_good_nums
                order_good.order = order_info
                order_good.save()

                order_mount += good.shop_price * cart_good_nums
                good.goods_num -= cart_good_nums  # 减少库存
                try:
                    cart_good.delete()
                except:
                    pass
                good.save()

        # 订单信息
        order_info.order_mount = order_mount
        order_info.save()

        return HttpResponse('{"status": "success", "msg": "订单创建成功"}', content_type='application/json')