using before and after update callback simultaneously on the same table

38 views
Skip to first unread message

黄祥

unread,
Aug 18, 2017, 3:30:01 AM8/18/17
to web2py-users
is it possible to using before and after update callback simultaneously on the same table? tested before but got an error traceback
e.g.
def __before_update_order(s, f):
query = (db.product.id == 1)
db(query).update(quantity = 9)

def __after_update_order(s, f):
if not ('is_active' in f and f['is_active'] == False):
s.update(notes = 'test')

db.order._before_update.append(__before_update_order)
db.order._after_update.append(__after_update_order)

return an error traceback :
RuntimeError: maximum recursion depth exceeded while calling a Python object

thanks and best regards,
stifan

Leonel Câmara

unread,
Aug 18, 2017, 5:02:13 AM8/18/17
to web2py-users
It's possible, but not if they're doing what yours are.

Namely the problem here is not that you have a before and after update callback. The problem is your __after_update_order. Your __after_update_order updates table order which triggers __after_update_order again. This would be ok if you had a condition that would terminate the recursion which you don't,  

Lets see what happens when s.update(notes='test') runs. This is an update where 'is_active' is not in f so it will trigger s.update(notes='test') again. And again. And again.  

黄祥

unread,
Aug 18, 2017, 5:54:50 PM8/18/17
to web2py-users
thanks leonel for the hints, find the solution with update_naive, because the logic for the real code is to recalculate the order table when it's update
e.g. use update_naive
def __after_update_order(s, f):
if not ('is_active' in f and f['is_active'] == False):
s.update_naive(notes = 'test')

from the book
For this purpose there the Set objects have an update_naive method that works like update but ignores before and after callbacks.

is there any side effect for this? tested it works smoothly, perhaps i can use compute function for that
Reply all
Reply to author
Forward
0 new messages