Hi all! Happy new year!
I’ve been experimenting with the one-to-many (link to table > table > multiple rows) Data Tables column and was trying to figure out how to add/remove items. First, I was trying to use list operators (ie. remove
, pop
, append
) on the column itself.
So I start out with this to get the items in the table and the row that I want to remove from the list of rows:
my_table_items = app_tables.mytable.search()
row_to_remove = app_tables.myothertable.get(name='my row')
Then I iterate through the rows, find the item to remove and then attempt to remove it using the remove()
list operator.
for m in my_table_items:
for list_item in m['my_column']:
if list_item == row_to_remove:
m['my_column'].remove(row_to_remove)
I also tried getting the index()
and using pop()
:
for m in my_table_items:
for list_item in m['my_column']:
if list_item == row_to_remove:
item_index = m['my_column'].index(row_to_remove)
m['my_column'].pop(item_index)
Neither of these produced any exceptions but both failed to remove the item from the list. Interestingly enough, index()
does return the correct index of the item in the list.
So then I tried assigning the column to a local variable, performing the operation, and then setting the column back to the local variable.
for m in my_table_items:
for list_item in m['my_column']:
if list_item == row_to_remove:
linked_rows_list = m['my_column']
linked_rows_list.remove(row_to_remove)
m['my_column'] = linked_rows_list
Boom! Works perfectly. I haven’t tried
append()
but I assume any of the list operators would also work this way.
Are list operations that add/delete not allowed directly on the data tables list object?
I’ve also tried the first method in server code so I don’t believe permissions are an issue.
Thanks, so much!
-Kirk