0

尝试在此处更改示例:https ://developers.google.com/appengine/docs/python/gettingstartedpython27/usingdatastore

但我想保存一个学生项目,而不是一个问候项目。在与之交互的静态目录中使用 AngularJS/app以获取数据。我只有一个 POST 和一个 GET,其中 POST 将保存一个条目,而 GET 将显示前 10 个条目。

当我 POST json 它似乎被保存时,响应是:

{"name": "name", "age": 22, "creator": "test@example.com"}

当我执行 GET 请求以获取列表但没有返回任何内容时,我得到:[]

查看我的代码和示例代码并来回查看我找不到区别。示例代码有效并保存了问候语,所以我的代码一定有问题。

import logging
import cgi
import urllib
import json
from google.appengine.api import users
from google.appengine.ext import ndb
import webapp2

DEFAULT_DS_NAME = 'default_ds'

def student_key(ds_name=DEFAULT_DS_NAME):
    return ndb.Key('Student', ds_name)

class StudentModel(ndb.Model):
    creator = ndb.UserProperty()
    name = ndb.StringProperty(indexed=False)
    age = ndb.IntegerProperty()

class Student(webapp2.RequestHandler):

    def post(self):
        st = json.loads(self.request.body)
        student = StudentModel(parent=student_key())
        if users.get_current_user():
            student.creator = users.get_current_user()
        student.name = st['name']
        student.age = st['age']
        key = student.put()
        logging.info(key.id())#logs a number
        st['creator']=student.creator.email()
        self.response.write(json.dumps(st));

    def get(self):
        student_query = StudentModel.query(
            ancestor=student_key()).order(-StudentModel.name)
        students = student_query.fetch(10)
        ret=[]
        for student in students:
            s={'name':'','age':0,'creator':''};
            if student.creator:
                s["creator"]=student.creator.email()
            s["name"]=student.name
            s["age"]=student.age
            logging.info("Pushing s");#never logs anything
            ret.push(s)
        self.response.write(json.dumps(ret))#returns []

application = webapp2.WSGIApplication([
    ('/app/student', Student)
], debug=True)
4

1 回答 1

1

You're trying to order query by unindexed field, so you need to set indexed=True to Student model:

name = ndb.StringProperty(indexed=True)
于 2013-10-06T07:50:46.980 回答