Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from google.appengine.ext import ndb
- # Datastore Model for a Student.
- class Student(ndb.Model):
- first_name = ndb.StringProperty()
- last_name = ndb.StringProperty()
- credits = ndb.IntegerProperty()
- ########################################
- # Modeling Relationships in Datastore #
- ########################################
- # Datastore Model for a Wand that stores a reference to its owner.
- class Wand(ndb.Model):
- core = ndb.StringProperty()
- wood_type = ndb.StringProperty()
- model = ndb.IntegerProperty()
- length = ndb.FloatProperty()
- # 'owner' is a reference to a Student via a Key.
- owner = ndb.KeyProperty(kind="Student")
- ##### Set up the data. Add a student and their wand.
- # Make a Student to represent Ginny Weasley.
- ginny = Student(first_name="Ginny", last_name="Weasley", credits=0)
- # Add to the datastore and store the Key that was returned.
- ginny_key = ginny.put()
- # Create a Wand for Ginny, using her Key as the 'owner' value.
- new_wand = Wand(core="voldemort", length=6.66, owner=ginny_key)
- # Add the wand to the datastore.
- new_wand_key = new_wand.put()
- ##### Find the wand owner's first name, by following its 'owner'.
- # 1. Get the Key stored in the wand.
- owner_key = new_wand.owner
- # 2. Use Key's .get() method to retrieve the full entity.
- owner_entity = owner_key.get()
- # 3. Print their first name.
- print owner_entity.first_name
- ###########
- # Queries #
- ###########
- # Uncomment to add some data to work with if your datastore is empty.
- # harry = Student(first_name="Harry", last_name="Potter", credits=45)
- # harry.put()
- # hermione = Student(first_name="Hermione", last_name="Granger", credits=100000)
- # hermione.put()
- ### Retrieve all entities of a certain class/kind with .query() and .fetch().
- # 1. Set up the query (the question).
- student_query = Student.query()
- # 2. Execute the query by calling .fetch() and store the list of results.
- all_students = student_query.fetch()
- # 3. Iterate over results and print them one per line.
- print
- print "All Students:"
- for student in all_students:
- print student
- ### Retrieve the first single matching entity by calling .get() instead of
- ### .fetch().
- first_student = student_query.get()
- print
- print "First student:"
- print student
- ### Filter results with .query(filter_expression)
- # Find the entities with more than 60 credits.
- student_filter_query = Student.query(Student.credits > 60)
- all_students = student_filter_query.fetch()
- print
- print "Students with more than 60 credits:"
- for student in all_students:
- print student
- ### Sort results with .order(sort_criteria).
- # Sort students by last name (alphabetically).
- student_ordered_query = student_query.order(Student.last_name)
- ordered_students = student_ordered_query.fetch()
- print
- print "Students listed alphabetically by last name:"
- for student in ordered_students:
- print student
- # You can reverse the sort by prefixing with a '-'.
- student_reverse_ordered_query = student_query.order(-Student.last_name)
- reverse_ordered_students = student_reverse_ordered_query.fetch()
- print
- print "Students listed alphabetically in reverse by last name:"
- for student in reverse_ordered_students:
- print student
- ### Limit results with .fetch(limit)
- # Get the first 5 students.
- five_students = student_query.fetch(limit=5)
- print
- print "First 5 students:"
- for student in five_students:
- print student
Add Comment
Please, Sign In to add comment