This is what a model looks like:
class Tweet(Model): userid = Field(hash_key=True) id = Field(range_key=True) ts = Field(data_type=datetime, index='ts-index') text = Field()
The model declares the fields an object has, their data types, and the schema of the table.
Since DynamoDB is a NoSQL database, you can attach arbitrary additional fields (undeclared fields) to the model, and they will be stored appropriately. For example, this tweet doesn’t declare a retweets field, but you could assign it anyway:
tweet.retweets = 7 tweet.sync()
Undeclared fields will not be saved if they begin or end with an underscore. This is intentional behavior so you can set local-only variables on your models.
tweet.retweets = 7 # this is saved to Dynamo tweet._last_updated = datetime.utcnow() # this is NOT saved to Dynamo
Since models define the schema of a table, you can use them to create or delete tables. Every model has a meta_ field attached to it which contains metadata about the model. This metadata object has the create and delete methods.
from dynamo3 import DynamoDBConnection connection = DynamoDBConnection.connect_to_region('us-east-1') Tweet.meta_.create_dynamo_schema(connection) Tweet.meta_.delete_dynamo_schema(connection)
You can also register your models with the engine and create all the tables at once:
engine.register(User, Tweet, Message) engine.create_schema()