Guest User

Untitled

a guest
Jul 19th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. from google.appengine.ext import ndb
  2.  
  3. # Datastore Model for a Student.
  4. class Student(ndb.Model):
  5. first_name = ndb.StringProperty()
  6. last_name = ndb.StringProperty()
  7. credits = ndb.IntegerProperty()
  8.  
  9. ########################################
  10. # Modeling Relationships in Datastore #
  11. ########################################
  12.  
  13. # Datastore Model for a Wand that stores a reference to its owner.
  14. class Wand(ndb.Model):
  15. core = ndb.StringProperty()
  16. wood_type = ndb.StringProperty()
  17. model = ndb.IntegerProperty()
  18. length = ndb.FloatProperty()
  19. # 'owner' is a reference to a Student via a Key.
  20. owner = ndb.KeyProperty(kind="Student")
  21.  
  22. ##### Set up the data. Add a student and their wand.
  23. # Make a Student to represent Ginny Weasley.
  24. ginny = Student(first_name="Ginny", last_name="Weasley", credits=0)
  25. # Add to the datastore and store the Key that was returned.
  26. ginny_key = ginny.put()
  27.  
  28. # Create a Wand for Ginny, using her Key as the 'owner' value.
  29. new_wand = Wand(core="voldemort", length=6.66, owner=ginny_key)
  30. # Add the wand to the datastore.
  31. new_wand_key = new_wand.put()
  32.  
  33. ##### Find the wand owner's first name, by following its 'owner'.
  34. # 1. Get the Key stored in the wand.
  35. owner_key = new_wand.owner
  36. # 2. Use Key's .get() method to retrieve the full entity.
  37. owner_entity = owner_key.get()
  38. # 3. Print their first name.
  39. print owner_entity.first_name
  40.  
  41. ###########
  42. # Queries #
  43. ###########
  44.  
  45. # Uncomment to add some data to work with if your datastore is empty.
  46. # harry = Student(first_name="Harry", last_name="Potter", credits=45)
  47. # harry.put()
  48. # hermione = Student(first_name="Hermione", last_name="Granger", credits=100000)
  49. # hermione.put()
  50.  
  51. ### Retrieve all entities of a certain class/kind with .query() and .fetch().
  52. # 1. Set up the query (the question).
  53. student_query = Student.query()
  54. # 2. Execute the query by calling .fetch() and store the list of results.
  55. all_students = student_query.fetch()
  56. # 3. Iterate over results and print them one per line.
  57. print
  58. print "All Students:"
  59. for student in all_students:
  60. print student
  61.  
  62. ### Retrieve the first single matching entity by calling .get() instead of
  63. ### .fetch().
  64. first_student = student_query.get()
  65. print
  66. print "First student:"
  67. print student
  68.  
  69. ### Filter results with .query(filter_expression)
  70. # Find the entities with more than 60 credits.
  71. student_filter_query = Student.query(Student.credits > 60)
  72. all_students = student_filter_query.fetch()
  73. print
  74. print "Students with more than 60 credits:"
  75. for student in all_students:
  76. print student
  77.  
  78. ### Sort results with .order(sort_criteria).
  79. # Sort students by last name (alphabetically).
  80. student_ordered_query = student_query.order(Student.last_name)
  81. ordered_students = student_ordered_query.fetch()
  82. print
  83. print "Students listed alphabetically by last name:"
  84. for student in ordered_students:
  85. print student
  86. # You can reverse the sort by prefixing with a '-'.
  87. student_reverse_ordered_query = student_query.order(-Student.last_name)
  88. reverse_ordered_students = student_reverse_ordered_query.fetch()
  89. print
  90. print "Students listed alphabetically in reverse by last name:"
  91. for student in reverse_ordered_students:
  92. print student
  93.  
  94. ### Limit results with .fetch(limit)
  95. # Get the first 5 students.
  96. five_students = student_query.fetch(limit=5)
  97. print
  98. print "First 5 students:"
  99. for student in five_students:
  100. print student
Add Comment
Please, Sign In to add comment