Add direct link in grid to edit page for connected table

35 views
Skip to first unread message

Nicola Clementel

unread,
Oct 25, 2016, 1:05:45 AM10/25/16
to web2py-users

Hi,


I have several tables connected in the following way:


db.define_table('table_1',

    Field('name',unique=True))


db.define_table('table_2',

    Field(....),

    .....

    Field('ref_t1','reference table_1',requires=IS_IN_DB(db,db.table_1.id,'%(name)s')))


db.define_table('table_3',

    Field(....),

    .....

    Field('ref_t2','reference table_2',requires=IS_IN_DB(db,db.table_2.id)))


db.define_table('table_4',

    Field(....),

    .....

    Field('ref_t2','reference table_2',requires=IS_IN_DB(db,db.table_2.id)))



Ignoring for the moment the connection with table_1, I would like to add in the view for table_2 a direct link to edit table_3 (every grid row of table_2 as a 1-to-1 connection with a single instance of table_3 or 4).


Using grid I'm trying to set a link this way:


grid=SQLFORM.grid(db.table_2,.....,links = [lambda row: A ('modify', _href=URL('index', args=['edit','table_3', db(db.table_3.ref_t2==row.id).select(db.table_3.id).first().id],user_signature=True),_class="btn")])


but it is not working and I can't understand why, since if I substitute "row.id" hardcoding the id of the row in the grid, it is working (clearly only for one row).

The error is:  <type 'exceptions.AttributeError'> 'NoneType' object has no attribute 'id'


An other option might be to change the link that display the children list in smartgrid to point directly to the edit page for the only child, but I'm not sure how to do it.


Any suggestion on how to solve this?


Thanks.



Nicola Clementel

unread,
Oct 25, 2016, 11:25:32 AM10/25/16
to web2py-users
Solved!

The problem was ignoring the connection with table_1: table_3 and 4 names are equal to the db.table_1.name values.

The following code is working. 

The only things I don't understand is why it is necessary to use the "try:" syntax.
Without it, even though the link is correctly created and returned, the line "test= ... " is creating an error.

Can someone explain me why?

Thanks!

Nicola

def index():

    grid
=SQLFORM.grid(db.table_2, create=False, editable=True, searchable=False, details= True, csv=False,links=[lambda row: getLink(row)])
   
return locals()


def getLink(row):
   
try:
        test
=db(db.table_1.id==row.ref_t1).select(db.table_1.name).first().name
   
except:
        link
=''
   
else:
       
if test =='table_3':
            tb
=db.table_3
       
else:
            tb
=db.table_4
        ID
=db(tb.ref_tb2==row.id).select(tb.id).first().id

        link
= A('modify',_href=URL('index',args=['edit',test,ID],user_signature=True),_class='btn btn-default')


Massimo Di Pierro

unread,
Oct 29, 2016, 10:36:45 PM10/29/16
to web2py-users
no idea but if this line fails:

        test=db(db.table_1.id==row.ref_t1).select(db.table_1.name).first().name

do you get a traceback that we can see? My guess is that occasionally db(....).select(..).first() is None and therefore there is .name
Reply all
Reply to author
Forward
0 new messages