@anvil.server.callable
def update_data(key,val):
my_data = app_tables.clients.get(client_id='fra_001')['contractor_data']
# this is a list of dictionaries
for cont in my_data:
if cont['change']:
cont[key] = val
app_tables.clients.get(client_id='fra_001')['contractor_data'] = my_data
I’ve also tried:
@anvil.server.callable
def update_data(key,val):
my_data = app_tables.clients.get(client_id='fra_001')['contractor_data']
# this is a list of dictionaries
for cont in my_data:
if cont['change']:
del cont[key]
cont[key] = val
# believe it or not this is the only method that works sometimes!
app_tables.clients.get(client_id='fra_001')['contractor_data'] = my_data
and…
@anvil.server.callable
def update_data(key,val):
my_row = app_tables.clients.get(client_id='fra_001')
my_data = my_row['contractor_data']
# this is a list of dictionaries
for cont in my_data:
if cont['change']:
cont[key] = val
my_row.update(contractor_data=my_data)
Sometimes some of the above work. Sometimes only one of them does. Sometimes none.
What am I missing?! I’m sure I’m missing an elegant way to once and for all handle the updating of simple objects in data tables!
If I understand what you’ve written, enumerate actually works on the original dictionary and doesn’t just extract it to another one as my normal for loop did?
You were looping through a list (that has indexes) that contained multiple dictionaries.
enumerate just allows you to loop through both the item in the list and capture the number of the index at the same time.
We are not working on the original dictionary, like @owen.campbell pointed out above, but instead we update the list with a new value if it needs to be updated, and then we update the entire row (just that one column really) with a new list if that has changed.
So you still end up operating on a copy, but then you check the ‘real’ one against it and change the ‘real’ one if the copy ends up being different that what you started with.
With the above you can update multiple entries at once by making that update dictionary any arbitrary length of keys and data. Duplicate keys will overwrite, and new ones will be created.