1 回答
TA贡献1828条经验 获得超3个赞
您将使用merge()函数,然后最终提交会话。
db.session.merge(order)
db.session.commit()
绕过它的两种方法。解决方案 2 永远是我的最佳选择
1.) 使用对象查询获取方法
@login_required()
def order_cancel(request):
if request.method == 'POST':
try:
order_id = request.POST.get('id')
print(order_id)
order = Order.objects.get(id=order_id)
charge_id = order.charge_id
refund = stripe.Refund.create(
charge=charge_id,
)
order.status = 'Canceled'
db.session.merge(order)
db.session.commit()
print(order.status)
print('success')
return HttpResponseRedirect(reverse_lazy('buying'))
except Order.DoesNotExist:
return redirect('home')
2.) 使用对象查询 filter_by 方法
@login_required()
def order_cancel(request):
if request.method == 'POST':
try:
order_id = request.POST.get('id')
print(order_id)
order = Order.query.filter_by(id=order_id).first()
charge_id = order.charge_id
refund = stripe.Refund.create(
charge=charge_id,
)
order.status = 'Canceled'
db.session.merge(order)
db.session.commit()
print(order.status)
print('success')
return HttpResponseRedirect(reverse_lazy('buying'))
except Order.DoesNotExist:
return redirect('home')
添加回答
举报