0

I have a custom controller in SQLFORM.grid and I am using links. The problem is (so it appears) that since I am using left join there when my custom function works, edit and view do not work.

So my links is the following

 links = [lambda row: A('',_class='glyphicon glyphicon glyphicon-remove-sign',
                    callback=URL('settings','deactivate',vars=dict(table='workers_skills',field = 'ws_status'
                    ,value = row.workers_skills.id )))]

my grid constructor

grid_workersskills= SQLFORM.grid(query=query,left=[db.workers.on(db.workers_skills.ws_worker==db.workers.id),
            db.skills.on(db.workers_skills.ws_skill==db.skills.id)], 
            fields=fields,  searchable=False, orderby=[db.workers.w_nick_name],create=True,
            deletable=False, editable=True, paginate=50, buttons_placement = 'right',
            showbuttontext = False,
            links = links, 
            #oncreate=myfunction,
            ui = dict(widget='',
              header='',
              content='',
              default='',
              cornerall='',
              cornertop='',
              cornerbottom='',
              button='button btn btn-default',
              buttontext='buttontext button',
              buttonadd='icon plus icon-plus glyphicon glyphicon-plus',
              buttonback='icon leftarrow icon-arrow-left glyphicon glyphicon-arrow-left',
              buttonexport='icon downarrow icon-download glyphicon glyphicon-download',
              buttondelete='icon trash icon-trash glyphicon glyphicon-trash',
              buttonedit='icon pen icon-pencil glyphicon glyphicon-pencil',
              buttontable='icon rightarrow icon-arrow-right glyphicon glyphicon-arrow-right',
              buttonview='icon magnifier icon-zoom-in glyphicon glyphicon-eye-open',
              ),
            exportclasses  = dict(csv_with_hidden_cols=False, html = False, tsv = False, tsv_with_hidden_cols=False, json = False))

the problem is caused by row.workers_skills.id

If i keep it this way my function deactivate works ok, but when I click on edit or view of the record I get an error

 Row attribute has no object workers_skills

If I put it to row.id the view and edit work, but my deactivate function not because ID or the record is not created.

I have been struggling with this for the last week. Please help

Thank you

Yebach
  • 1,661
  • 8
  • 31
  • 58

1 Answers1

3

When the grid involves a join, you must refer to fields within a row via the row.table.field format. However, when you view/edit a record from such a grid, only the record from a single table is shown (i.e., there is no longer a join involved), so you must now refer to fields within a row via the row.field format. Because your "link" appears in the grid as well as in the view/edit page, whichever format you use to refer to the id field, it will result in an error in one of those two contexts.

So, all you need to do is ensure your code works in both contexts. Here's a simple trick:

Instead of:

row.workers_skills.id

do:

row.get('workers_skills', row).id

The .get() method attempts to retrieves the 'workers_skills' key, but if it doesn't exist, it instead returns a default value, which in this case is just the original row. It then retrieves the "id" field of that returned object (either the original row or the row.workers_skills sub-row).

Anthony
  • 25,466
  • 3
  • 28
  • 57