简体   繁体   English

Google App Engine数据存储区上的Python API查询

[英]Python API Query on Google App Engine Datastore

I am having trouble with the getting the correct response for a Python Query on Google App Engine. 我在为Google App Engine上的Python查询获取正确的响应时遇到麻烦。

Here is the LOG from GAE, it successfully prints out the object that matches the query parameters, but now I am trying to send that objects data as JSON back. 这是GAE的LOG,它成功打印出与查询参数匹配的对象,但是现在我正尝试将这些对象数据作为JSON发送回去。 I am creating a rudimentary user auth system that queries for a USER object using email and password, and if it exists than it returns all the data back. 我正在创建一个基本的用户身份验证系统,该系统使用电子邮件和密码查询USER对象,如果存在,则它将所有数据返回。

How can I break down this query to return the data that was found? 如何分解该查询以返回找到的数据?

E 00:21:06.352 Encountered unexpected error from ProtoRPC method implementation: AttributeError ('list' object has no attribute 'email')
  Traceback (most recent call last):
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/protorpc-1.0/protorpc/wsgi/service.py", line 181, in protorpc_service_app
      response = method(instance, request)
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/endpoints-1.0/endpoints/api_config.py", line 1332, in invoke_remote
      return remote_method(service_instance, request)
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/protorpc-1.0/protorpc/remote.py", line 412, in invoke_remote_method
      response = method(service_instance, request)
    File "/base/data/home/apps/s~caramel-theory-800/1.381271940209425490/photoswap_api.py", line 34, in user_auth
      return UserCreateResponseMessage(email=query.email, password=query.password, username=query.username,
  AttributeError: 'list' object has no attribute 'email'
E 00:21:06.360 [User(key=Key('User', 5086441721823232), email=u'pop', password=u'top', username=u'yop')]

Here is the ENDPOINT API 这是ENDPOINT API

I believe the issue is within the last line of code where it trys to return the results of the query (UserAuthResponseMessage).. 我认为问题出在尝试返回查询结果(UserAuthResponseMessage)的代码的最后一行。

@endpoints.method(UserAuthRequestMessage, UserAuthResponseMessage,
                  path='user', http_method='GET',
                  name='user.auth')
def user_auth(self, request):
    # create some type of query to check for email address, and than check to see if passwords match
    query = User.query(User.email == request.email, User.password == request.password).fetch()
    print query

    # return the info from the server
    return UserCreateResponseMessage(email=query[0].email, password=query[0].password, username=query[0].username,
                                     id=query[0].key.id())


APPLICATION = endpoints.api_server([PhotoswapAPI], restricted=False)

You problem is your are performing a query and expecting to reference the email property of the query which of course it doesn't have one. 您的问题是您正在执行查询,并且期望引用查询的电子邮件属性,而该属性当然没有。

If you expect only one result from the query then you should be using get rather than index addressing. 如果只希望查询得到一个结果,则应该使用get而不是索引寻址。

The other problem is the user_auth method included doesn't match your stack trace. 另一个问题是所包含的user_auth方法与您的堆栈跟踪不匹配。 So there is a problem here. 所以这里有个问题。

With the help of Tim Hoffman this is the solution that I was able to come up with. 在蒂姆·霍夫曼(Tim Hoffman)的帮助下,这是我能够想到的解决方案。

I was returning the wrong ResponseMessage, and I was better off using .get instead of .fetch as there should only be 1 result returned. 我返回了错误的ResponseMessage,最好使用.get而不是.fetch,因为应该只返回1个结果。

@endpoints.api(name='photoswap', version='v1')
class PhotoswapAPI(remote.Service):
    @endpoints.method(UserCreateRequestMessage, UserCreateResponseMessage,
                      path='user', http_method='POST',
                      name='user.create')
    def user_create(self, request):
        entity = User(email=request.email, password=request.password, username=request.username)
        entity.put()
        return UserCreateResponseMessage(email=entity.email, password=entity.password, username=entity.username,
                                         id=entity.key.id())

    @endpoints.method(UserAuthRequestMessage, UserAuthResponseMessage,
                      path='user', http_method='GET',
                      name='user.auth')
    def user_auth(self, request):
        # create some type of query to check for email address, and than check to see if passwords match
        query = User.query(User.email == request.email, User.password == request.password).get()
        print query

        # return the info from the server
        return UserAuthResponseMessage(email=query.email, password=query.password, username=query.username, id=query.key.id())


APPLICATION = endpoints.api_server([PhotoswapAPI], restricted=False)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM