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

Python 如果在外部方法中执行事务,如何查看更新的模型对象,python,django,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 Ba

以下是我遇到的情况:

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()
中提取/缓存的值?

您需要使用
@transaction.commit\u手动
装饰程序。了解交易 因此,您需要使用
@transaction.commit\u手动将付费呼叫移动到视图中

    @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的 事务中间件

它是这样工作的:当一个请求启动时,Django启动一个 交易如果生成的响应没有问题,Django 提交任何挂起的事务。如果view函数生成 例外情况是,Django回滚所有挂起的事务


应该可以了;)

这已经发生在
user.deposit\u funds()
方法中,它发生在一个单独的函数/文件中。@David542那么你的问题是什么?@David542那么在
user.deposit\u funds()
方法中,你没有保存新值,因为在第二次打印时,您应该获取新值。它保存在事务中。您需要显示实际的源代码,而不是用您认为正在发生的事情来混淆您的问题。您所描述的操作应该已经起作用。回答时需要
存款
的源代码。请注意
存款
现在是
支付
方法。什么是self.user?如果支付是用户的一种方法,并且您希望更新该对象,则用户应该是self。
self。user
就是用户。
pay
方法是另一种模式,即
CreditCard
。这并不能回答这个问题。要评论或要求作者澄清,请在他们的帖子下方留下评论-你可以随时在自己的帖子上发表评论,一旦你有足够的评论,你就可以发表评论。@BhargavRao你比赛迟到了。该问题已被编辑多次,当这两个答案发布时,它们不仅是相关的,而且回答了问题,看起来像是书面问题的正确答案。@Two Bitalchest Brother如果你是2k以上的代表,你就知道审查队列了。:((最好把这个留在这里)
    user.balance = new_balance
    user.save()