目前,我在models.py
中使用了这种方法 def charge(self, amount):
self.balance = (self.balance - Decimal(amount))
self.save()
当有新请求进来时,我会检查该用户是否有 current_balance> =价格所以我可以执行objectRow.charge(price)问题是当我获得1000个相同ID的请求时结束了-000负余额,用户最终还欠我钱,这不是重点。
到目前为止,这是我的验证。
accountObject = account.objects.get(auth_token=auth_token)
if accountObject.balance >= settings.MT_COST:
# Save
ProductObject = product(application=applicationObject,
account=accountObject,
cost=settings.MT_COST,
direction=2,
status_log=1,
product=product_id,
ip_address=ip_address)
ProductObject.save()
# Charge User
ProductObject.charge(settings.MT_COST)
data = {'status': "ok"}
return HttpResponse(json.dumps(data), content_type='application/json', status=200)
# Not enough Balance
else:
data = {'status': "error"}
return HttpResponse(json.dumps(data), content_type='application/json', status=406)