本文整理汇总了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)