Databases Reference
In-Depth Information
The GAE data store can be thought of as an object store where each entity is an object. That means
data store entities or members could be instances of a Python class, like Task . The class name, Task ,
translates to an entity kind. A key uniquely identifi es an entity among all entities in the data store. A
key is a combined identifi er that includes:
Inheritance path
Entity kind
Entity ID or entity key name
Hypothetically, that means if an entity of the BaseTask kind is a parent of a Task entity, then the
inheritance path for the Task entity includes references to the parent entity of the BaseTask kind.
Task itself becomes the entity kind. A specifi c entity of the kind, Task , has an ID, which can be
thought of as the primary key. An ID can be either of the following:
Application provided value, named key_name , which is a string
System-generated (i.e., GAE data store) unique numeric ID
So, you could create and save an entity as follows:
task = Task(name = “Design task manager app”,
description = “Design the task management application.
Create the initial blueprint and the app architecture.”,
start_date = datetime.datetime.now().date())
task.put()
Available for
download on
Wrox.com
taskmanager GAE project
This creates a task instance. The instance was created by passing values for name , description , and
start_date to the constructor. Alternatively, you could create an instance and then assign values
to the properties of that instance. You need to pass in values to the constructor for all required
properties at instantiation time. Values to the non-mandatory properties can be assigned using
either of the methods: via the constructor or via property assignments.
In the preceding example, no value was passed in for the key_name property so the data store
created a unique numeric ID for the entity. You can query for the key like so:
my_entity_key = task.key()
The output is a numeric value appended to the kind, which in this case is Task . Alternatively, you
could create a key for an entity and pass that in at creation time. Say you wanted to use task1 as
the key for an entity of the Task kind, you could instantiate a task entity like so:
another_task = Task(key_name = “task1”,
name = “Yet another task”,
description = “Yet another task is, as the name says, yet another task.”,
start_date = datetime.datetime(2011, 2, 1, 12, 0, 0).date())
Now, querying for the key using another_task.key() returns Task: task1 , which includes the
key_name you assigned at the time of creation.
Search WWH ::




Custom Search