On Github google-cssi / datastore-slides
Created by Luis Ibanez / @luisibanez
"students":[ {"firstName":"John", "lastName":"Doe", "email":"jd@gmail.com"}, {"firstName":"Anna", "lastName":"Smith", "email":"aj@gmail.com"}, {"firstName":"Peter","lastName":"Jones", "email":"pj@gmail.com"} ]
"firstName":"John"
{ "firstName":"John", "lastName":"Doe", "email":"jd@gmail.com" }
"students":[ {"firstName":"John", "lastName":"Doe", "email":"jd@gmail.com"}, {"firstName":"Anna", "lastName":"Smith", "email":"aj@gmail.com"}, {"firstName":"Peter","lastName":"Jones", "email":"pj@gmail.com"} ]
"students":[ {"firstName":"John", "lastName":"Doe", "email":"jd@gmail.com"}, {"firstName":"Anna", "lastName":"Smith", "email":"aj@gmail.com"}, ... ]
"classes":[ {"name":"math 101", "grade":"5th", "room":"B11"} {"name":"zombies 201", "grade":"7th", "room":"Basement"} ... ]
Tell us about Python Classes!
class Student: age = 25 name = "John Doe" university = "UNC"
one_student = Student()
print one_student.age print one_student.name print one_student.university
one_student = Student()
print one_student.age
one_student.age = 22
print one_student.age
class Student: def __init__(self, name, age, university): self.name = name self.age = age self.university = university
one_student = Student(name="Mary", age=23, university="MIT")
print one_student.age print one_student.name print one_student.university
class Student: age = 25 name = "John Doe" university = "UNC"
def printMe(self): print 'name: ', self.name print 'age: ', self.age print 'university: ', self.university
one_student = Student(name="Leroy", age=20, university="UCLA") one_student.printMe()
Write a Python Class
Representing a Song
Write Methods
from google.appengine.ext import ndb
class Student(ndb.Model):
name = ndb.StringProperty(required=True)
university = ndb.StringProperty(required=True)
age = ndb.IntegerProperty(required=False)
student_instance = Student(name="Jon Doe", university="MIT")
student_instance.put()
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
Datastore Consistency
dev_appserver.py . --datastore_consistency_policy=consistent
Eventual Consistency...
Strong Consistency...
--datastore_consistency_policy=
consistent
time
random
key = student_instance.put()
key.get()
key.get().name
self.response.write(key.get().name)
AppEngine Datastore Interface
Record Key and Id
key = student_instance.put()
id = key.id()
student = Student.get_by_id(id)
key = ndb.Key('Student', id)
key.get()
key.get().name
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))
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()
https://cloud.google.com/appengine/docs/python/ndb/queryclass
https://cloud.google.com/appengine/docs/python/ndb/modelclass