查询所有实体的谷歌应用程序引擎数据存储 [英] Query google app engine datastore for all entities
问题描述
给定这个模型类
class Student(ndb.Model):
student_id = ndb.IntegerProperty(必需的= True)
student_name = ndb.StringProperty(required = True)
score = ndb.IntegerProperty(required = True)
$ b $ def toJSON(self):
jsonData = {
Student Id:str(self.student_id),
Name:self.student_name,
Score:str(self.score)
}
返回json.encode(jsonData)
我试图运行一个查询来返回所有学生姓名,以及每个学生的JSON格式的分数。
我已经在数据存储上运行了一个查询,并能够使用
class ViewStudentDetailsHandler(webapp2.RequestHandler):
def get(self):
student_id = self.request.get ('id')
callback = self.request.get('callback')
student = Student.get_by_id(studen t_id)
如果学生:
if(callback):
self.response.write(callback +'('+ student.toJSON()+')')
else:
self.response.write(student.toJSON())
else:
if(callback):
self.response.write(callback +(null))
else:
self.response.write(没有该学生的ID)
<但不知道如何获得 ALL
的学生。我已阅读示例,但是我仍然输了。我知道这一次我需要一个循环,但是这只是我能想出的所有东西。想法会被认可。
您将需要执行一个查询,并取决于有多少实体将其全部返回给一个请求将不可能或不切实际。然后您需要在查询中使用游标。
您应该阅读ndb文档中的查询部分 - 他们清楚需要做什么 -
对所有项目进行简单查询并将所需的详细信息作为Json记录列表返回,您将使用查询的map方法执行以下操作,它调用提供的函数或类方法。它不指望实体的方法,这就是为什么我不直接使用toJSON。
def回调(学生):
返回student.toJSON())
结果= Student.query()。map(callback)
您可能需要使用toJSON方法,看看运行它时的结果。 results
也可能需要显式转换为json,因此您可能希望将显式json.encode推迟到运行查询后。
Given this model class
class Student(ndb.Model):
student_id = ndb.IntegerProperty(required=True)
student_name = ndb.StringProperty(required=True)
score=ndb.IntegerProperty(required=True)
def toJSON(self):
jsonData = {
"Student Id":str(self.student_id),
"Name":self.student_name,
"Score": str(self.score)
}
return json.encode(jsonData)
I am trying to run a query to return all the student names, along with the score for each student in JSON format.
I already ran a query on the datastore and was able to retrieve information regarding each student using
class ViewStudentDetailsHandler(webapp2.RequestHandler):
def get(self):
student_id=self.request.get('id')
callback = self.request.get('callback')
student = Student.get_by_id(student_id)
if student:
if (callback):
self.response.write(callback + '(' + student.toJSON() + ')')
else:
self.response.write(student.toJSON())
else:
if(callback):
self.response.write(callback + "(null)")
else:
self.response.write("No student with that id")
But have no clue how to get ALL
of the students.I have read examples given by Google, but am still lost.I know this time around i'll need a loop, but that's about all i can come up with.Any ideas would be appreaciated.
You will need to perform a query, and depending how many entities returning all them in a single request will either not be possible or practical. You will then need to use cursors with the query.
You should read the section of Queries in the ndb docs - they are clear about what needs to be done - https://developers.google.com/appengine/docs/python/ndb/queries
A simple query for all items and to return the details you want as a list of Json records you would do the following, using the map method of a query, which calls the supplied function or classmethod. It doesn't expect a method of the entity thats why I don't use toJSON directly.
def callback(student):
return student.toJSON())
results = Student.query().map(callback)
You may need to fiddle with your toJSON method, have a look at what results looks like when you run it. results
may also need to explicitly converted to json, so you may want to defer the explicit json.encode till after after you have run the query.
这篇关于查询所有实体的谷歌应用程序引擎数据存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!