如果在外部方法中执行事务,如何查看更新的模型对象

时间:2015-01-26 18:09:23

标签: python django

以下是我遇到的情景:

user = User.objects.get(pk=1)
print 'Original Balance is %s' user.balance

### this commits the new balance in a transaction in another file/function
user.pay(1.05) # this function does a user.balance=new_balance;user.save()

print 'New Balance is %s' user.balance

以下是pay()方法:

def pay(self, amount):
    """
    Withdraw `amount` USD from this credit card.
    The user's balance goes UP!!!
    """
    user = self.user
    environment_url = braintree.Configuration.environment._Environment__server.lower()
    print '>>> MODE: %s| AMOUNT: %s' % (environment_url, amount)

    with transaction.commit_manually():
        try:
            sale = braintree.Transaction.sale({
                    "amount": "%.2f" % float(amount),
                    "customer_id": self.braintree_id,
                })
            if sale.is_success:
                user.balance = float(user.balance) + amount
                user.save()
                payment = Payment.objects.create(
                   user=user,
                   amount_in=amount,
                   status=PAID,
                   transaction_id=sale.transaction.id,
                   domain=braintree.Configuration.environment._Environment__server,
                   data = str(sale),
                   reason='Deposit via api of $%.2f' % amount)
                History.objects.create(user=self.user, history_type=HISTORY_TYPE_DEPOSIT, obj_class_name='Payment', obj_pk=payment.pk)
                transaction.commit()
                return sale

如何更新用户对象的值,而不是使用从第一个.get()中提取/缓存的值?

2 个答案:

答案 0 :(得分:2)

您需要使用@transaction.commit_manually装饰器。阅读有关交易的Django docs  因此,您需要使用@transaction.commit_manually装饰器

将付费电话转移到视图
    @transaction.commit_manually
    def viewfunc(request):
        user = User.objects.get(pk=1)
        print 'Original Balance is %s' user.balance

        ### this commits the new balance in a transaction in another file/function
        user.pay(1.05)
        transaction.commit()
        print 'New Balance is %s' user.balance

然后,你的薪水功能。

    def pay(self, amount):
        """
        Withdraw `amount` USD from this credit card.
        The user's balance goes UP!!!
        """
        user = self.user
        environment_url = braintree.Configuration.environment._Environment__server.lower()
        print '>>> MODE: %s| AMOUNT: %s' % (environment_url, amount)


                sale = braintree.Transaction.sale({
                        "amount": "%.2f" % float(amount),
                        "customer_id": self.braintree_id,
                    })
                if sale.is_success:
                    user.balance = float(user.balance) + amount
                    user.save()
                    payment = Payment.objects.create(
                       user=user,
                       amount_in=amount,
                       status=PAID,
                       transaction_id=sale.transaction.id,
                       domain=braintree.Configuration.environment._Environment__server,
                       data = str(sale),
                       reason='Deposit via api of $%.2f' % amount)
                    History.objects.create(user=self.user, history_type=HISTORY_TYPE_DEPOSIT, obj_class_name='Payment', obj_pk=payment.pk)

                    return sale
  

处理Web请求中的事务的推荐方法是绑定   他们通过Django的请求和响应阶段   TransactionMiddleware。

     

它的工作原理如下:当一个请求开始时,Django启动了一个   交易。如果生成的响应没有问题,Django   提交任何待处理的交易。如果视图函数产生一个   例外,Django回滚任何待处理的交易。

答案 1 :(得分:0)

    user.balance = new_balance
    user.save()

应该这样做;)