Linked data tables

I can’t work out how to fetch related data from a linked table.
table1 has a text column (“Name”) and a link to a single row from table2 (“Status”) which has 2 possible text values, Enabled & Disabled.
table1 data was entered via the IDE and the linked data was selected from a popup radio button choice.

I have two server side functions -

@anvil.server.callable
def fetch_all_switches():
  return app_tables.switches.search()

@anvil.server.callable
def fetch_switch_status(id):
  row=app_tables.statuses.get_by_id(id)
  return row

I call the first one -

switchlist=anvil.server.call('fetch_all_switches')
for s in switchlist:

At this stage, s[‘Name’] has data. As I cannot print s[‘Status’] I assume it is a reference ID into table2 (“Statuses”) so I try -

status=anvil.server.call('fetch_switch_status',row['Status'])

but the returned value is not usable as far as I can see.

How do I get that linked data?

Hi David,

In fact, row['Status'] is actually already the thing you’re looking for. Anvil has done the work for you, and row['Status'] is the actual linked row from the statuses table. That means that if you have a column "Description" in the statuses table, you can just do:

for s in switchlist:
    status = s["Status"]["Description"]

It’s that easy! I hope it makes sense, please let me know if not, or if I’ve missed the point.

- Ian

2 Likes

Thanks, Ian.

I saw that being suggested in the data tables help page (the line says print n[“Person”][“Name”]) but I just didn’t make the connection to what I was doing.

I’ll get there :slight_smile:

Is it possible to use this notation to bind a column in DataGrid elegantly?

Edit: just found the solution - by adding a label to Data Row Panel and setting its binding, d’oh…