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.
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')