查询所有实体的谷歌应用引擎数据存储 [英] Query google app engine datastore for all entities

查看:24
本文介绍了查询所有实体的谷歌应用引擎数据存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定这个模型类

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)

我正在尝试运行查询以 JSON 格式返回所有学生姓名以及每个学生的分数.

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")

但不知道如何获得ALL的学生.我已经阅读了示例 由 Google 提供,但我仍然迷路了.我知道这一次我需要一个循环,但这就是我能想到的全部内容.任何想法都会得到认可.

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.

您应该阅读 ndb 文档中的查询部分 - 他们清楚需要做什么 - https://developers.google.com/appengine/docs/python/ndb/queries

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

对所有项目的简单查询并返回您想要的详细信息作为 Json 记录列表,您将执行以下操作,使用查询的 map 方法,该方法调用提供的函数或类方法.它不期望实体的方法,这就是我不直接使用 toJSON 的原因.

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)

您可能需要修改您的 toJSON 方法,看看运行它时的结果是什么样的.results 可能还需要显式转换为 json,因此您可能希望将显式 json.encode 推迟到运行查询之后.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆