def addOrder(request): orderids = request.session.get('orderids') order = OrderInfo() oOder = OrderInfo.objects.all().order_by('-oid')[0:1] if len(oOder) == 0: order.oid = 1 else: order.oid = int(oOder[0].oid) + 1 order.odate = datetime.now() order.oIsPay = 0 order.ototal = request.POST.get('total') order.oaddress = request.POST.get('address') order.user_id = request.session.get('user_id') order.zhifu = request.POST.get('zhifu') order.save() for oid in orderids: cartInfo = CartInfo.objects.get(id=oid) good = GoodsInfo.objects.get(id=cartInfo.goods_id) if cartInfo.count > good.gkucun: return JsonResponse({'status': 2}) detail = OrderDetailInfo() detail.price = good.gprice detail.count = cartInfo.count detail.goods_id = good.id detail.order_id = order.oid detail.save() return JsonResponse({'status': 1})
def addOrder(request):#先获取下单的所有购物车的id orderids = request.session.get('orderids') #构建一个订单对象 order = OrderInfo() #查询订单最大的id oOder = OrderInfo.objects.all().order_by('oid')[0:1] if len(oOder) == 0: order.oid = 1 else: print(int(oOder[0].oid)) order.oid = int(oOder[0].oid)+1 #增加订单时间 order.odate = datetime.now() #是否付款 order.oIsPay =0 order.ototal=request.POST.get('totle') order.oaddress= request.POST.get('address') order.user_id= request.session.get('user_id') order.zhifu= request.POST.get('zhifu') order.save() #增加订单明细的商品洗信息 for oid in orderids:#获取购物车(会有多条数据) cartInfo = CartInfo.objects.get(id=oid) good = GoodInfo.objects.get(id=cartInfo.goods_id) #如果返回2表示库存不够 if cartInfo.count>good.gkucun: return JsonResponse({'status':2}) #定义一个明细订单 detail = OrderDetailInfo() detail.price = good.gprice detail.count = cartInfo.count detail.goods_id = good.id detail.price = order.oid detail.save() #如果返回1表示增加成功 return JsonResponse({'status':1})