3.3. Update an ItemΒΆ

awstin provides an update expression syntax that allows update expressions to be built and chained together with &. The awstin.dynamodb.Attr.set(), awstin.dynamodb.Attr.remove(), awstin.dynamodb.Attr.add(), and awstin.dynamodb.Attr.delete() methods correspond to the update operations available in DynamoDB.

awstin.dynamodb.Table.update_item() takes the primary key as the first argument.

from models import Movie

from awstin.dynamodb import DynamoDB


def update_movie(title, year, rating, plot, actors):
    dynamodb = DynamoDB()
    table = dynamodb[Movie]

    update_expression = (
        Movie.info.rating.set(rating)
        & Movie.info.plot.set(plot)
        & Movie.info.actors.set(actors)
    )

    return table.update_item(
        key=(year, title),
        update_expression=update_expression,
    )


if __name__ == "__main__":
    update_response = update_movie(
        "The Big New Movie",
        2015,
        5.5,
        "Everything happens all at once.",
        ["Larry", "Moe", "Curly"],
    )
    print("Update movie succeeded:")
    print(update_response.serialize())