当正在运行的应用程序处理ViewDogs的get请求时,返回的错误是500内部服务器错误。在GAE Stack Driver控制台,我得到:
NeedIndexError: no matching index found - recommended index is:
- kind:Dog
ancestor: yes
properties: -
name: id
direction: desc".
我确认有一个名为Dog的索引加载,它当前显示“Serving”
同时确认已成功创建了4只狗,并且可以从DataStore实体选项卡中单独查询它们并使用SELECT * FROM Dog查询它们。另请注意注释掉的行self.response.write('You dog!')
,该行在取消注释时会按预期返回,因此可能不是路径问题。我可以通过GET返回一只狗(此代码省略)ViewAllDogs类中的代码可能有问题,使用GAE文档尽力而为。
index.yaml文件,我确认已上传并具有状态:“服务”
indexes:
- kind: Dog
ancestor: yes
properties:- name: id
direction: desc
- name: name
direction: desc
- name: type
direction: desc
- name: weight
direction: desc
- name: boarded
direction: desc
app.yaml文件:
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /.*
script: main.app
main.py
import webapp2
from datetime import datetime
from google.appengine.ext import ndb
import webbrowser
import json
def gql_json_parser(query_obj):
result = []
for entry in query_obj:
result.append(dict([(p, unicode(getattr(entry, p))) for p in entry.properties()]))
return result
class Dog(ndb.Model):
""" Models an individual Dog """
id = ndb.StringProperty(required = True, indexed = True)
name = ndb.StringProperty(required = True, indexed = True)
type = ndb.StringProperty(required = True, indexed = True)
weight = ndb.IntegerProperty(required = True, indexed = True)
boarded = ndb.BooleanProperty(required = True, indexed = True)
@classmethod
def query_dog(cls, ancestor_key):
return cls.query(ancestor=ancestor_key).order(-cls.id)
class ViewAllDogs(webapp2.RequestHandler):
def get(self):
# self.response.write('You dog!')
parent_dog = self.request.get('parent_dog')
ancestor_key = ndb.Key("Dog", parent_dog or '*noDogs*')
query_data = Dog.query_dog(ancestor_key).fetch(10)
json_query_data = gql_json_parser(query_data)
self.response.headers['Content-Type'] = 'application/json'
self.response.write(json.dumps(json_query_data))
app = webapp2.WSGIApplication([
('/ViewDogs', ViewAllDogs)
], debug=True)
类似的问题比比皆是,包括这个问题Query google app engine datastore for all entities,但没有一个问题解决我的问题。感谢。
答案 0 :(得分:0)
像往常一样,我让它变得比需要的要复杂得多......
import webapp2, json
from datetime import datetime
from google.appengine.ext import ndb
MAXDOGS = 100
class Dog(ndb.Model):
""" Models an individual Dog """
id = ndb.StringProperty(required = True, indexed = True)
name = ndb.StringProperty(required = True, indexed = True)
type = ndb.StringProperty(required = True, indexed = True)
weight = ndb.IntegerProperty(required = True, indexed = True)
boarded = ndb.BooleanProperty(required = True, indexed = True)
class ViewAllDogs(webapp2.RequestHandler):
def get(self):
query_data = Dog.query()
results = query.fetch(limit = MAX_SLIPS)
aList = []
for match in results:
aList.append({'id': match.id, 'name': match.name, 'type': match.type,
'weight': match.weight, 'boarded': match.boarded})
self.response.headers['Content-Type'] = 'application/json'
self.response.write(json.dumps(aList))
app = webapp2.WSGIApplication([
('/ViewDogs', ViewAllDogs)