2012-08-31 51 views
1

我在質疑自己關於新peewee的功能:commit_on_success裝飾。@commit_on_success並保存()

  1. 如何確定函數是否爲「成功」?

  2. 如果我使用這個裝飾器,是否需要在我的修改過的對象上調用save()

從我試過,我認爲#2是肯定的,但我只是想確保,因爲我沒有找到一個明確的答案peewee文檔中的這兩個問題。

回答

2

我認爲你的意思是裝飾者實際上被稱爲commit_on_success,並作爲Database類的方法實現。

這個想法是,假設你想在請求/響應中轉移一些錢。這裏是一個非常做作例如:

db = SqliteDatabase('my_database.db') 

@db.commit_on_success 
def process_transfer_request(request, from_id, to_id, amount): 
    from_acct = Account.get(id=from_id) 
    to_acct = Account.get(id=to_id) 
    from_acct.balance -= amount 
    to_acct.balance += amount 
    from_acct.save() 
    to_acct.save() 
    return Response('success! the money was transferred') 

如果有異常ANYWHERE在功能提高,沒有錢將被轉移以及將引發異常。

否則,如果沒有異常提出,錢將被轉移並返回響應。

+0

好吧,你會確保'.save()'不會完成如果異常引發正確嗎? – shkschneider