Skip to main content

update_field

Update one field value from a single record.

update_success: bool = table_client.update_field(
key_value=str, field_path=str, query_kwargs=Optional[dict]
)

Parameters#

Property nameRequiredAccepted typesDefaultDescription
key_valueYESAny-The path expression to target the attribute to set/update in your record. See Field path selectors
field_pathYESstr-The path expression to target the attribute to set/update in your record. See Field path selectors
value_to_setYESAnyThe value that will be set/update the attribute you selected with the field_path property.
query_kwargsNOdictNoneUsed to pass data to populate a field_path that contains keys. See example below :

Availability#

TableAvailable
DynamoDBBasicTable✅
DynamoDBCachingTable✅
ExternalDynamoDBApiBasicTable✅
ExternalDynamoDBApiCachingTable✅

Queried record#

{
"userId": "x42",
"username": "Robinson"
}

Code#

from typing import Optional
from StructNoSQL import TableDataModel, DynamoDBBasicTable, PrimaryIndex, BaseField
class UsersTableModel(TableDataModel):
userId = BaseField(field_type=str, required=True)
username = BaseField(field_type=str, required=False)
class UsersTable(DynamoDBBasicTable):
def __init__(self):
primary_index = PrimaryIndex(hash_key_name='userId', hash_key_variable_python_type=str)
super().__init__(
table_name='accounts-data', region_name='eu-west-2',
data_model=UsersTableModel, primary_index=primary_index,
auto_create_table=True
)
table_client = UsersTable()
update_success: bool = table_client.update_field(
key_value='x42', field_path='username', value_to_set='Paul'
)
print(f"Update success : {update_success}")
retrieved_new_name: Optional[str] = table_client.get_field(
key_value='x42', field_path='username'
)
print(f"Retrieved new name : {retrieved_new_name}")

Output#

Update success : True
Retrieved new name : Paul