Created by Luis Ibanez / @luisibanez
Launching AppEngine App
dev_appserver.py --port 9080 --admin_port 9000 .
Checking for updates to the SDK. Starting API server at: http://localhost:59242 Starting module "default" running at: http://localhost:9080 Starting admin server at: http://localhost:9000
AppEngine Admin
AppEngine Admin Interface
AppEngine Datastore Interface
AppEngine Datastore Interface
AppEngine Datastore Interface
AppEngine Datastore Interface
Record Key and Id
key = student_instance.put()
student_instance_ref = key.get()
key.get().name
self.response.write(key.get().name)
key = student_instance.put()
id = key.id()
student = Student.get_by_id(id)
key = ndb.Key('Student', id)
key.get()
key.get().name
key.delete()
Get id and navigate to that url
self.redirect('/view_student?key=%s' % key.urlsafe())
Get an entity from an ID
key = ndb.Key(urlsafe=self.request.get("key")) student = key.get()
student_query = Student.query()
student_data = student_query.fetch()
template_values = { 'students' : student_data }
template = JINJA_ENVIRONMENT.get_template('index.html') self.response.write(template.render(template_values))
student_query = Student.query()
student_query = student_query.filter(Student.name == 'Larry')
student_data = student_query.fetch()
student_query = Student.query()
student_query = student_query.filter(Student.university == 'MIT')
student_data = student_query.fetch()
student_query = Student.query()
student_query = student_query.filter(Student.university == 'MIT')
student_query = student_query.order(Student.age)
student_data = student_query.fetch()
student_query = Student.query()
student_query = student_query.filter(Student.university == 'MIT')
student_query = student_query.order(-Student.age)
student_data = student_query.fetch()
student_query = Student.query()
student_query = student_query.filter(Student.university == 'MIT')
student_data = student_query.fetch(15)
student_query = Student.query()
student_query = student_query.filter(Student.university == 'MIT')
student_query = student_query.order(-Student.age)
student_data = student_query.fetch(15)
https://cloud.google.com/appengine/docs/python/ndb/queryclass
https://cloud.google.com/appengine/docs/python/ndb/modelclass
The Lunchbox is Mine!
class Lunchbox(ndb.Model):
foods = ndb.StringProperty(repeated=True)
drink = ndb.StringProperty()
insulated = ndb.BooleanProperty()
logo = ndb.BlobProperty()
class Kindergartener(ndb.Model):
name = ndb.StringProperty()
birthday = ndb.DateProperty()
fav_food = ndb.StringProperty()
lunchbox_key = ndb.KeyProperty(Lunchbox)
lunchbox_instance = Lunchbox( foods = ["Apple", "Sandwich", "Brocoli"], drink = "Carrot Juice", insulated = True, logo = "string with logo image encoded")
my_lunchbox_key = lunchbox_instance.put()
kid_instance = Kindergartener( name = "Bart Simpson", birthday = datetime.strptime('Jun 1 2005', '%b %d %Y'), fav_food = "Cheeseburger", lunchbox_key = my_lunchbox_key)
kid_key = kid_instance.put()
kid_query = Kindergartener.query() kid_query = kid_query.filter(Kindergartener.name == 'Bart Simpson')
kid_data = kid_query.fetch()
my_lunchbox_key = kid_data[0].lunchbox_key my_lunchbox = my_lunchbox_key.get()
kid_query = Kindergartener.query() kid_query = kid_query.filter( Kindergartener.lunchbox_key == my_lunchbox_key)
kid_data = kid_query.fetch()
kid_name = kid_data[0].name
Unus pro omnibus!
class Team(ndb.Model): city = ndb.StringProperty() name = ndb.StringProperty()
class Player(ndb.Model): team = ndb.KeyProperty(kind='Team') last_name= ndb.StringProperty() position = ndb.StringProperty()
da_bulls= Team(city='Chicago', name='Bulls') bulls_key = da_bulls.put()
drose = Player(team=bulls_key, last_name='Rose', position='Point Guard') joakim = Player(team=bulls_key, last_name='Noah', position='Center')
drose.put() joakim.put()
player_query = Player.query(Player.team == bulls_key)
player_data = player_query.fetch()
for player in player_data: self.response.out.write('<p>'+player.last_name+'</p>')
Omnes pro uno
class Player(ndb.Model): last_name= ndb.StringProperty() position = ndb.StringProperty()
class Team(ndb.Model): city = ndb.StringProperty() name = ndb.StringProperty() player_keys = ndb.KeyProperty(kind='Player', repeated=True)
drose = Player(last_name='Rose', position='Point Guard') drose_key = drose.put()
joakim = Player(last_name='Noah', position='Center') joakim_key = joakim.put()
da_bulls = Team(city='Chicago', name='Bulls') da_bulls.player_keys.append(drose_key) da_bulls.player_keys.append(joakim_key) da_bulls.put()
bulls_query = Team.query(Team.name == 'Bulls') bulls_data = bulls_query.fetch()
da_bulls = bulls_data[0]
for player_key in da_bulls.player_keys: self.response.out.write('<p>'+player_key.get().last_name+'</p>')
class Fan(ndb.Model): first_name = ndb.StringProperty() last_name = ndb.StringProperty() home_city = ndb.StringProperty()
class FanTeam(ndb.Model): fan = ndb.KeyProperty(Fan) team = ndb.KeyProperty(Team)
nicki = Fan(first_name='Nicki', last_name='Anselmo', home_city='New Orleans').put() norah = Fan(first_name='Norah', last_name='Solo', home_city='New York').put()
FanTeam(fan=nicki, team=da_bulls).put() FanTeam(fan=norah, team=da_bulls).put()
bulls_query = FanTeam.query() bulls_query = bulls_query.filter(FanTeam.team==da_bulls) bulls_fans = bulls_query.fetch()
for fan_team in bulls_fans: fan_key = fan_team.get().fan self.response.out.write('<p>'+fan_key.get().last_name+'</p>')