當前位置: 首頁>>代碼示例>>Python>>正文


Python Order.save方法代碼示例

本文整理匯總了Python中models.order.Order.save方法的典型用法代碼示例。如果您正苦於以下問題:Python Order.save方法的具體用法?Python Order.save怎麽用?Python Order.save使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在models.order.Order的用法示例。


在下文中一共展示了Order.save方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: post

# 需要導入模塊: from models.order import Order [as 別名]
# 或者: from models.order.Order import save [as 別名]
    def post(self, request):
        # 添加訂單
        try:
            customer = request.u
            note = request.json.get("note")
            delivery_information_id = request.json.get("delivery_information_id")
            store_id = request.json.get("store_id")
            food_list = request.json.get("food_list")
            store = Store.objects.get(id=store_id)
            total_price = 0

            if not all([food_list, delivery_information_id, store]):
                return JsonErrorResponse("food_list, delivery_information_id, store_id are needed", 400)

            # 檢查food_list
            assert isinstance(food_list, list) and len(food_list) > 0, "food_list format wrong"
            # 檢查庫存+計算價格
            for order_food in food_list:
                food = store.foods.get(id=order_food['food_id'])
                food_count = int(order_food['count'])
                total_price += food.price * food_count
                assert food.stock > food_count, "food stock is not enough"
            # 檢查收貨信息
            delivery_information = customer.delivery_informations.get(id=delivery_information_id)
            # 檢查賬戶類型
            assert request.account_type == "customer", "only customer can make order"

            # 創建order
            new_order = Order(
                note=note,
                total_price=total_price,
                customer=customer,
                store=store,
                delivery_information=delivery_information
            )
            new_order.save()

            # 減少庫存,創建order_food
            for order_food in food_list:
                food = store.foods.get(id=order_food['food_id'])
                food_count = int(order_food['count'])
                new_stock = food.stock - food_count
                store.foods.filter(id=order_food['food_id']).update(stock=new_stock)
                OrderFood(
                    count=food_count,
                    food=food,
                    order=new_order
                ).save()

        except Exception, e:
            print e
            return JsonErrorResponse("Fail:" + e.message)
開發者ID:BillBillBillBill,項目名稱:Take-out,代碼行數:54,代碼來源:views.py


注:本文中的models.order.Order.save方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。