Get reference format representation on dal callbacks

58 views
Skip to first unread message

Carlos Cesar Caballero Díaz

unread,
Dec 6, 2017, 11:31:57 AM12/6/17
to web...@googlegroups.com
Hi, I am building a module (plugin) that is using some "after insert"
and "after uptade" dal callbacks. The tables has references (relations)
and I need to obtain the referenced table string format instead of it's
id in the callback, and I need it to be generic. How can I achieve that?

Greetings.

Massimo Di Pierro

unread,
Dec 16, 2017, 4:44:04 PM12/16/17
to web2py-users
Can you please provide a complete example of what you want using pseudocode?

Carlos Cesar Caballero Díaz

unread,
Dec 19, 2017, 8:48:15 AM12/19/17
to web...@googlegroups.com

Hi Massimo, thanks for your answer, basically I need the row object instead of the set in the after update callback, I have managed to do this passing the table object to my plugin and making a query. But there is a way to know the table that belongs an after update callback without passing explicitly the table object?

Let's say something like:

model:

from mymodule import my_after_update_callback

db.mytable._after_update.append(my_after_update_callback)

mymodule:

def my_after_update_callback(s, f):

    row = get_callback_row()

    table = get_callback_table()


There is another thing:

Related to this, I am working with the experimental haystack plugin for whoosh, but I have made some changes (for using the whoosh backend with python3 and other things), but I look that the Github project page has no updates and there are unattended pull requests. Is the haystack plugin dead?

Greetings.

El 16/12/17 a las 16:44, Massimo Di Pierro escribió:

Anthony

unread,
Dec 19, 2017, 11:49:29 AM12/19/17
to web...@googlegroups.com
On Tuesday, December 19, 2017 at 8:48:15 AM UTC-5, Carlos Cesar Caballero wrote:

Hi Massimo, thanks for your answer, basically I need the row object instead of the set in the after update callback, I have managed to do this passing the table object to my plugin and making a query. But there is a way to know the table that belongs an after update callback without passing explicitly the table object?

Let's say something like:

model:

from mymodule import my_after_update_callback

db.mytable._after_update.append(my_after_update_callback)

mymodule:

def my_after_update_callback(s, f):

    row = get_callback_row()

    table = get_callback_table()



Updates do not involve fetching the updated records from the database, so the DAL does not have any Row objects to provide to the callback. If you need the updated records, you'll have to fetch them yourself within the callback based on the Set:

rows = s.select()

Getting a table name is more complicated. From the Set object, you can extract the Query object used to identify the records to be updated. You can then get any tables involved with the query via:

tables = db._adapter.tables(s.query)

tables will be a dictionary with table names as keys and Table objects as values.

Note, I don't think db._adapter.tables is considered to be part of the public API, so probably not guaranteed to remain backward compatible in the future.

Anthony

Carlos Cesar Caballero Díaz

unread,
Dec 22, 2017, 1:11:12 PM12/22/17
to web...@googlegroups.com

Thank you so much!!!

Greetings.


El 19/12/17 a las 11:49, Anthony escribió:
On Tuesday, December 19, 2017 at 8:48:15 AM UTC-5, Carlos Cesar Caballero wrote:

Hi Massimo, thanks for your answer, basically I need the row object instead of the set in the after update callback, I have managed to do this passing the table object to my plugin and making a query. But there is a way to know the table that belongs an after update callback without passing explicitly the table object?

Let's say something like:

model:

from mymodule import my_after_update_callback

db.mytable._after_update.append(my_after_update_callback)

mymodule:

def my_after_update_callback(s, f):

    row = get_callback_row()

    table = get_callback_table()



Updates do not involve fetching the updated records from the database, so the DAL does not have any Row objects to provide to the callback. If you need the updated records, you'll have to fetch them yourself within the callback based on the Set:

rows = set.select()

Regarding the tablename, you can extract it from the Set as well. In most cases, something like the following should work:

table = set.query.first.table

Note, that will be the actual DAL Table object -- if you want the name, you can do set.query.first.tablename.

Anthony
Reply all
Reply to author
Forward
0 new messages