Recall that the Python datastore API represents entities using
objects of classes named after kinds. Example 5-1
shows Python code that creates three
for an online role-playing game.
Example 5-1. Python code to create several entities of the kind Player
from google.appengine.ext import db import datetime class Player(db.Expando): pass player1 = Player(name='wizard612', level=1, score=32, charclass='mage', create_date=datetime.datetime.now()) player1.put() player2 = Player(name='druidjane', level=10, score=896, charclass='druid', create_date=datetime.datetime.now()) player2.put() player3 = Player(name='TheHulk', level=7, score=500, charclass='warrior', create_date=datetime.datetime.now()) player3.put()
Once again, we’ll use
Expando to keep the examples
simple. As we start talking about queries, the importance of using a
consistent layout, or schema, for entities of a kind
will become apparent.
The Python API provides two ways to formulate queries, one using an object-oriented interface and one based on GQL.
The first way to formulate a query is with an instance of the
Query class. A
Query object can be constructed
in one of two ways, with equivalent results:
q = db.Query(Player) q = Player.all()
In both cases,
q is assigned a new
instance that represents all entities of the kind
Player. The query is not executed right away; right now it’s just a question waiting to be asked. Without filters or sort orders, the object represents a ...