API

High Level API

DynamoDB Models for PynamoDB

class pynamodb.models.Model(hash_key=None, range_key=None, **attributes)

Defines a PynamoDB Model

This model is backed by a table in DynamoDB. You can create the table by with the create_table method.

exception DoesNotExist(msg=None, cause=None)

Raised when an item queried does not exist

classmethod Model.add_throttle_record(records)

(Experimental) Pulls out the table name and capacity units from records and puts it in self.throttle

Parameters:records – A list of usage records
classmethod Model.batch_get(items, consistent_read=None, attributes_to_get=None)

BatchGetItem for this model

Parameters:items – Should be a list of hash keys to retrieve, or a list of tuples if range keys are used.
classmethod Model.batch_write(auto_commit=True)

Returns a context manager for a batch operation’

Parameters:auto_commit – Commits writes automatically if True
classmethod Model.count(hash_key=None, range_key_condition=None, filter_condition=None, consistent_read=False, index_name=None, limit=None, **filters)

Provides a filtered count

Parameters:
  • hash_key – The hash key to query. Can be None.
  • range_key_condition – Condition for range key
  • filter_condition – Condition used to restrict the query results
  • consistent_read – If True, a consistent read is performed
  • index_name – If set, then this index is used
  • filters – A dictionary of filters to be used in the query. Requires a hash_key to be passed.
classmethod Model.create_table(wait=False, read_capacity_units=None, write_capacity_units=None)

Create the table for this model

Parameters:
  • wait – If set, then this call will block until the table is ready for use
  • read_capacity_units – Sets the read capacity units for this table
  • write_capacity_units – Sets the write capacity units for this table
Model.delete(condition=None, conditional_operator=None, **expected_values)

Deletes this object from dynamodb

classmethod Model.delete_table()

Delete the table for this model

classmethod Model.describe_table()

Returns the result of a DescribeTable operation on this model’s table

classmethod Model.dump(filename)

Writes the contents of this model’s table as JSON to the given filename

classmethod Model.dumps()

Returns a JSON representation of this model’s table

classmethod Model.exists()

Returns True if this table exists, False otherwise

classmethod Model.from_raw_data(data)

Returns an instance of this class from the raw data

Parameters:data – A serialized DynamoDB object
classmethod Model.get(hash_key, range_key=None, consistent_read=False)

Returns a single object using the provided keys

Parameters:
  • hash_key – The hash key of the desired item
  • range_key – The range key of the desired item, only used when appropriate.
classmethod Model.get_throttle()

Returns the throttle implementation for this Model

classmethod Model.query(hash_key, range_key_condition=None, filter_condition=None, consistent_read=False, index_name=None, scan_index_forward=None, conditional_operator=None, limit=None, last_evaluated_key=None, attributes_to_get=None, page_size=None, **filters)

Provides a high level query API

Parameters:
  • hash_key – The hash key to query
  • range_key_condition – Condition for range key
  • filter_condition – Condition used to restrict the query results
  • consistent_read – If True, a consistent read is performed
  • index_name – If set, then this index is used
  • limit – Used to limit the number of results returned
  • scan_index_forward – If set, then used to specify the same parameter to the DynamoDB API. Controls descending or ascending results
  • conditional_operator
  • last_evaluated_key – If set, provides the starting point for query.
  • attributes_to_get – If set, only returns these elements
  • page_size – Page size of the query to DynamoDB
  • filters – A dictionary of filters to be used in the query
classmethod Model.rate_limited_scan(filter_condition=None, attributes_to_get=None, segment=None, total_segments=None, limit=None, conditional_operator=None, last_evaluated_key=None, page_size=None, timeout_seconds=None, read_capacity_to_consume_per_second=10, allow_rate_limited_scan_without_consumed_capacity=None, max_sleep_between_retry=10, max_consecutive_exceptions=30, consistent_read=None, **filters)

Scans the items in the table at a definite rate. Invokes the low level rate_limited_scan API.

Parameters:
  • filter_condition – Condition used to restrict the scan results
  • attributes_to_get – A list of attributes to return.
  • segment – If set, then scans the segment
  • total_segments – If set, then specifies total segments
  • limit – Used to limit the number of results returned
  • conditional_operator
  • last_evaluated_key – If set, provides the starting point for scan.
  • page_size – Page size of the scan to DynamoDB
  • filters – A list of item filters
  • timeout_seconds – Timeout value for the rate_limited_scan method, to prevent it from running infinitely
  • read_capacity_to_consume_per_second – Amount of read capacity to consume every second
  • allow_rate_limited_scan_without_consumed_capacity – If set, proceeds without rate limiting if the server does not support returning consumed capacity in responses.
  • max_sleep_between_retry – Max value for sleep in seconds in between scans during throttling/rate limit scenarios
  • max_consecutive_exceptions – Max number of consecutive provision throughput exceeded exceptions for scan to exit
  • consistent_read – If True, a consistent read is performed
Model.refresh(consistent_read=False)

Retrieves this object’s data from dynamodb and syncs this local object

Parameters:consistent_read – If True, then a consistent read is performed.
Model.save(condition=None, conditional_operator=None, **expected_values)

Save this object to dynamodb

classmethod Model.scan(filter_condition=None, segment=None, total_segments=None, limit=None, conditional_operator=None, last_evaluated_key=None, page_size=None, consistent_read=None, **filters)

Iterates through all items in the table

Parameters:
  • filter_condition – Condition used to restrict the scan results
  • segment – If set, then scans the segment
  • total_segments – If set, then specifies total segments
  • limit – Used to limit the number of results returned
  • conditional_operator
  • last_evaluated_key – If set, provides the starting point for scan.
  • page_size – Page size of the scan to DynamoDB
  • filters – A list of item filters
  • consistent_read – If True, a consistent read is performed
Model.update(attributes=None, actions=None, condition=None, conditional_operator=None, **expected_values)

Updates an item using the UpdateItem operation.

Parameters:attributes

A dictionary of attributes to update in the following format {

attr_name: {‘value’: 10, ‘action’: ‘ADD’}, next_attr: {‘value’: True, ‘action’: ‘PUT’},

}

Model.update_item(attribute, value=None, action=None, condition=None, conditional_operator=None, **expected_values)

Updates an item using the UpdateItem operation.

This should be used for updating a single attribute of an item.

Parameters:

PynamoDB attributes

class pynamodb.attributes.Attribute(hash_key=False, range_key=False, null=None, default=None, attr_name=None)

An attribute of a model

deserialize(value)

Performs any needed deserialization on the value

serialize(value)

This method should return a dynamodb compatible value

class pynamodb.attributes.BinaryAttribute(hash_key=False, range_key=False, null=None, default=None, attr_name=None)

A binary attribute

deserialize(value)

Returns a decoded string from base64

serialize(value)

Returns a base64 encoded binary string

class pynamodb.attributes.BinarySetAttribute(hash_key=False, range_key=False, null=None, default=None, attr_name=None)

A binary set

deserialize(value)

Returns a decoded string from base64

serialize(value)

Returns a base64 encoded binary string

class pynamodb.attributes.BooleanAttribute(hash_key=False, range_key=False, null=None, default=None, attr_name=None)

A class for boolean attributes

class pynamodb.attributes.JSONAttribute(hash_key=False, range_key=False, null=None, default=None, attr_name=None)

A JSON Attribute

Encodes JSON to unicode internally

deserialize(value)

Deserializes JSON

serialize(value)

Serializes JSON to unicode

class pynamodb.attributes.LegacyBooleanAttribute(hash_key=False, range_key=False, null=None, default=None, attr_name=None)

A class for legacy boolean attributes

Previous versions of this library serialized bools as numbers. This class allows you to continue to use that functionality.

class pynamodb.attributes.MapAttribute(**attributes)

A Map Attribute

The MapAttribute class can be used to store a JSON document as “raw” name-value pairs, or it can be subclassed and the document fields represented as class attributes using Attribute instances.

To support the ability to subclass MapAttribute and use it as an AttributeContainer, instances of MapAttribute behave differently based both on where they are instantiated and on their type. Because of this complicated behavior, a bit of an introduction is warranted.

Models that contain a MapAttribute define its properties using a class attribute on the model. For example, below we define “MyModel” which contains a MapAttribute “my_map”:

class MyModel(Model):
my_map = MapAttribute(attr_name=”dynamo_name”, default={})

When instantiated in this manner (as a class attribute of an AttributeContainer class), the MapAttribute class acts as an instance of the Attribute class. The instance stores data about the attribute (in this example the dynamo name and default value), and acts as a data descriptor, storing any value bound to it on the attribute_values dictionary of the containing instance (in this case an instance of MyModel).

Unlike other Attribute types, the value that gets bound to the containing instance is a new instance of MapAttribute, not an instance of the primitive type. For example, a UnicodeAttribute stores strings in the attribute_values of the containing instance; a MapAttribute does not store a dict but instead stores a new instance of itself. This difference in behavior is necessary when subclassing MapAttribute in order to access the Attribute data descriptors that represent the document fields.

For example, below we redefine “MyModel” to use a subclass of MapAttribute as “my_map”:

class MyMapAttribute(MapAttribute):
my_internal_map = MapAttribute()
class MyModel(Model):
my_map = MyMapAttribute(attr_name=”dynamo_name”, default = {})

In order to set the value of my_internal_map on an instance of MyModel we need the bound value for “my_map” to be an instance of MapAttribute so that it acts as a data descriptor:

MyModel().my_map.my_internal_map = {‘foo’: ‘bar’}

That is the attribute access of “my_map” must return a MyMapAttribute instance and not a dict.

When an instance is used in this manner (bound to an instance of an AttributeContainer class), the MapAttribute class acts as an AttributeContainer class itself. The instance does not store data about the attribute, and does not act as a data descriptor. The instance stores name-value pairs in its internal attribute_values dictionary.

Thus while MapAttribute multiply inherits from Attribute and AttributeContainer, a MapAttribute instance does not behave as both an Attribute AND an AttributeContainer. Rather an instance of MapAttribute behaves EITHER as an Attribute OR as an AttributeContainer, depending on where it was instantiated.

So, how do we create this dichotomous behavior? Using the AttributeContainerMeta metaclass. All MapAttribute instances are initialized as AttributeContainers only. During construction of AttributeContainer classes (subclasses of MapAttribute and Model), any instances that are class attributes are transformed from AttributeContainers to Attributes (via the _make_attribute method call).

deserialize(values)

Decode as a dict.

class pynamodb.attributes.MapAttributeMeta(name, bases, attrs)

This is only here for backwards compatibility: i.e. so type(MapAttribute) == MapAttributeMeta

class pynamodb.attributes.NumberAttribute(hash_key=False, range_key=False, null=None, default=None, attr_name=None)

A number attribute

deserialize(value)

Decode numbers from JSON

serialize(value)

Encode numbers as JSON

class pynamodb.attributes.NumberSetAttribute(hash_key=False, range_key=False, null=None, default=None, attr_name=None)

A number set attribute

class pynamodb.attributes.SetMixin

Adds (de)serialization methods for sets

deserialize(value)

Deserializes a set

serialize(value)

Serializes a set

Because dynamodb doesn’t store empty attributes, empty sets return None

class pynamodb.attributes.UTCDateTimeAttribute(hash_key=False, range_key=False, null=None, default=None, attr_name=None)

An attribute for storing a UTC Datetime

deserialize(value)

Takes a UTC datetime string and returns a datetime object

serialize(value)

Takes a datetime object and returns a string

class pynamodb.attributes.UnicodeAttribute(hash_key=False, range_key=False, null=None, default=None, attr_name=None)

A unicode attribute

serialize(value)

Returns a unicode string

class pynamodb.attributes.UnicodeSetAttribute(hash_key=False, range_key=False, null=None, default=None, attr_name=None)

A unicode set

element_serialize(value)

This serializes unicode / strings out as unicode strings. It does not touch the value if it is already a unicode str :param value: :return:

PynamoDB Indexes

class pynamodb.indexes.AllProjection

An ALL projection

class pynamodb.indexes.GlobalSecondaryIndex

A global secondary index

class pynamodb.indexes.IncludeProjection(non_attr_keys=None)

An INCLUDE projection

class pynamodb.indexes.Index

Base class for secondary indexes

classmethod count(hash_key, range_key_condition=None, filter_condition=None, consistent_read=False, **filters)

Count on an index

classmethod query(hash_key, range_key_condition=None, filter_condition=None, scan_index_forward=None, consistent_read=False, limit=None, last_evaluated_key=None, attributes_to_get=None, **filters)

Queries an index

class pynamodb.indexes.IndexMeta(name, bases, attrs)

Index meta class

This class is here to allow for an index Meta class that contains the index settings

class pynamodb.indexes.KeysOnlyProjection

Keys only projection

class pynamodb.indexes.LocalSecondaryIndex

A local secondary index

class pynamodb.indexes.Projection

A class for presenting projections

Low Level API

PynamoDB lowest level connection

class pynamodb.connection.Connection(region=None, host=None, session_cls=None, request_timeout_seconds=None, max_retry_attempts=None, base_backoff_ms=None)

A higher level abstraction over botocore

batch_get_item(table_name, keys, consistent_read=None, return_consumed_capacity=None, attributes_to_get=None)

Performs the batch get item operation

batch_write_item(table_name, put_items=None, delete_items=None, return_consumed_capacity=None, return_item_collection_metrics=None)

Performs the batch_write_item operation

client

Returns a botocore dynamodb client

create_table(table_name, attribute_definitions=None, key_schema=None, read_capacity_units=None, write_capacity_units=None, global_secondary_indexes=None, local_secondary_indexes=None, stream_specification=None)

Performs the CreateTable operation

delete_item(table_name, hash_key, range_key=None, condition=None, expected=None, conditional_operator=None, return_values=None, return_consumed_capacity=None, return_item_collection_metrics=None)

Performs the DeleteItem operation and returns the result

delete_table(table_name)

Performs the DeleteTable operation

describe_table(table_name)

Performs the DescribeTable operation

dispatch(operation_name, operation_kwargs)

Dispatches operation_name with arguments operation_kwargs

Raises TableDoesNotExist if the specified table does not exist

get_attribute_type(table_name, attribute_name, value=None)

Returns the proper attribute type for a given attribute name :param value: The attribute value an be supplied just in case the type is already included

get_conditional_operator(operator)

Returns a dictionary containing the correct conditional operator, validating it first.

get_consumed_capacity_map(return_consumed_capacity)

Builds the consumed capacity map that is common to several operations

get_exclusive_start_key_map(table_name, exclusive_start_key)

Builds the exclusive start key attribute map

get_expected_map(table_name, expected)

Builds the expected map that is common to several operations

get_identifier_map(table_name, hash_key, range_key=None, key='Key')

Builds the identifier map that is common to several operations

get_item(table_name, hash_key, range_key=None, consistent_read=False, attributes_to_get=None)

Performs the GetItem operation and returns the result

get_item_attribute_map(table_name, attributes, item_key='Item', pythonic_key=True)

Builds up a dynamodb compatible AttributeValue map

get_item_collection_map(return_item_collection_metrics)

Builds the item collection map

get_meta_table(table_name, refresh=False)

Returns a MetaTable

get_query_filter_map(table_name, query_filters)

Builds the QueryFilter object needed for the Query operation

get_return_values_map(return_values)

Builds the return values map that is common to several operations

list_tables(exclusive_start_table_name=None, limit=None)

Performs the ListTables operation

parse_attribute(attribute, return_type=False)

Returns the attribute value, where the attribute can be a raw attribute value, or a dictionary containing the type: {‘S’: ‘String value’}

put_item(table_name, hash_key, range_key=None, attributes=None, condition=None, expected=None, conditional_operator=None, return_values=None, return_consumed_capacity=None, return_item_collection_metrics=None)

Performs the PutItem operation and returns the result

query(table_name, hash_key, range_key_condition=None, filter_condition=None, attributes_to_get=None, consistent_read=False, exclusive_start_key=None, index_name=None, key_conditions=None, query_filters=None, conditional_operator=None, limit=None, return_consumed_capacity=None, scan_index_forward=None, select=None)

Performs the Query operation and returns the result

rate_limited_scan(table_name, filter_condition=None, attributes_to_get=None, page_size=None, limit=None, conditional_operator=None, scan_filter=None, exclusive_start_key=None, segment=None, total_segments=None, timeout_seconds=None, read_capacity_to_consume_per_second=10, allow_rate_limited_scan_without_consumed_capacity=None, max_sleep_between_retry=10, max_consecutive_exceptions=10, consistent_read=None)

Performs a rate limited scan on the table. The API uses the scan API to fetch items from DynamoDB. The rate_limited_scan uses the ‘ConsumedCapacity’ value returned from DynamoDB to limit the rate of the scan. ‘ProvisionedThroughputExceededException’ is also handled and retried.

Parameters:
  • table_name – Name of the table to perform scan on.
  • filter_condition – Condition used to restrict the scan results
  • attributes_to_get – A list of attributes to return.
  • page_size – Page size of the scan to DynamoDB
  • limit – Used to limit the number of results returned
  • conditional_operator
  • scan_filter – A map indicating the condition that evaluates the scan results
  • exclusive_start_key – If set, provides the starting point for scan.
  • segment – If set, then scans the segment
  • total_segments – If set, then specifies total segments
  • timeout_seconds – Timeout value for the rate_limited_scan method, to prevent it from running infinitely
  • read_capacity_to_consume_per_second – Amount of read capacity to consume every second
  • allow_rate_limited_scan_without_consumed_capacity – If set, proceeds without rate limiting if the server does not support returning consumed capacity in responses.
  • max_sleep_between_retry – Max value for sleep in seconds in between scans during throttling/rate limit scenarios
  • max_consecutive_exceptions – Max number of consecutive ProvisionedThroughputExceededException exception for scan to exit
  • consistent_read – enable consistent read
requests_session

Return a requests session to execute prepared requests using the same pool

scan(table_name, filter_condition=None, attributes_to_get=None, limit=None, conditional_operator=None, scan_filter=None, return_consumed_capacity=None, exclusive_start_key=None, segment=None, total_segments=None, consistent_read=None)

Performs the scan operation

session

Returns a valid botocore session

update_item(table_name, hash_key, range_key=None, actions=None, attribute_updates=None, condition=None, expected=None, return_consumed_capacity=None, conditional_operator=None, return_item_collection_metrics=None, return_values=None)

Performs the UpdateItem operation

update_table(table_name, read_capacity_units=None, write_capacity_units=None, global_secondary_index_updates=None)

Performs the UpdateTable operation

class pynamodb.connection.TableConnection(table_name, region=None, host=None, session_cls=None, request_timeout_seconds=None, max_retry_attempts=None, base_backoff_ms=None)

A higher level abstraction over botocore

batch_get_item(keys, consistent_read=None, return_consumed_capacity=None, attributes_to_get=None)

Performs the batch get item operation

batch_write_item(put_items=None, delete_items=None, return_consumed_capacity=None, return_item_collection_metrics=None)

Performs the batch_write_item operation

create_table(attribute_definitions=None, key_schema=None, read_capacity_units=None, write_capacity_units=None, global_secondary_indexes=None, local_secondary_indexes=None, stream_specification=None)

Performs the CreateTable operation and returns the result

delete_item(hash_key, range_key=None, condition=None, expected=None, conditional_operator=None, return_values=None, return_consumed_capacity=None, return_item_collection_metrics=None)

Performs the DeleteItem operation and returns the result

delete_table()

Performs the DeleteTable operation and returns the result

describe_table()

Performs the DescribeTable operation and returns the result

get_item(hash_key, range_key=None, consistent_read=False, attributes_to_get=None)

Performs the GetItem operation and returns the result

put_item(hash_key, range_key=None, attributes=None, condition=None, expected=None, conditional_operator=None, return_values=None, return_consumed_capacity=None, return_item_collection_metrics=None)

Performs the PutItem operation and returns the result

query(hash_key, range_key_condition=None, filter_condition=None, attributes_to_get=None, consistent_read=False, exclusive_start_key=None, index_name=None, key_conditions=None, query_filters=None, limit=None, return_consumed_capacity=None, scan_index_forward=None, conditional_operator=None, select=None)

Performs the Query operation and returns the result

rate_limited_scan(filter_condition=None, attributes_to_get=None, page_size=None, limit=None, conditional_operator=None, scan_filter=None, segment=None, total_segments=None, exclusive_start_key=None, timeout_seconds=None, read_capacity_to_consume_per_second=None, allow_rate_limited_scan_without_consumed_capacity=None, max_sleep_between_retry=None, max_consecutive_exceptions=None, consistent_read=None)

Performs the scan operation with rate limited

scan(filter_condition=None, attributes_to_get=None, limit=None, conditional_operator=None, scan_filter=None, return_consumed_capacity=None, segment=None, total_segments=None, exclusive_start_key=None, consistent_read=None)

Performs the scan operation

update_item(hash_key, range_key=None, actions=None, attribute_updates=None, condition=None, expected=None, conditional_operator=None, return_consumed_capacity=None, return_item_collection_metrics=None, return_values=None)

Performs the UpdateItem operation

update_table(read_capacity_units=None, write_capacity_units=None, global_secondary_index_updates=None)

Performs the UpdateTable operation and returns the result

Exceptions

exception pynamodb.exceptions.PynamoDBConnectionError(msg=None, cause=None)

A base class for connection errors

exception pynamodb.exceptions.DeleteError(msg=None, cause=None)

Raised when an error occurs deleting an item

exception pynamodb.exceptions.QueryError(msg=None, cause=None)

Raised when queries fail

exception pynamodb.exceptions.ScanError(msg=None, cause=None)

Raised when a scan operation fails

exception pynamodb.exceptions.PutError(msg=None, cause=None)

Raised when an item fails to be created

exception pynamodb.exceptions.UpdateError(msg=None, cause=None)

Raised when an item fails to be updated

exception pynamodb.exceptions.GetError(msg=None, cause=None)

Raised when an item fails to be retrieved

exception pynamodb.exceptions.TableError(msg=None, cause=None)

An error involving a dynamodb table operation

exception pynamodb.exceptions.TableDoesNotExist(table_name)

Raised when an operation is attempted on a table that doesn’t exist

exception pynamodb.exceptions.DoesNotExist(msg=None, cause=None)

Raised when an item queried does not exist