【问题标题】:Python API Query on Google App Engine DatastoreGoogle App Engine Datastore 上的 Python API 查询
【发布时间】:2015-01-04 15:12:13
【问题描述】:

我无法在 Google App Engine 上获取 Python 查询的正确响应。

这是来自 GAE 的 LOG,它成功打印出与查询参数匹配的对象,但现在我正尝试将这些对象数据作为 JSON 发送回来。我正在创建一个基本的用户身份验证系统,该系统使用电子邮件和密码查询 USER 对象,如果存在则返回所有数据。

如何分解此查询以返回找到的数据?

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')]

这里是 ENDPOINT API

我认为问题出在它尝试返回查询结果的最后一行代码 (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)

【问题讨论】:

    标签: python google-app-engine


    【解决方案1】:

    您的问题是您正在执行查询并期望引用查询的电子邮件属性,当然它没有。

    如果您希望查询只有一个结果,那么您应该使用 get 而不是索引寻址。

    另一个问题是包含的user_auth 方法与您的堆栈跟踪不匹配。所以这里有问题。

    【讨论】:

      【解决方案2】:

      在 Tim Hoffman 的帮助下,这是我想出的解决方案。

      我返回了错误的 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)
      

      【讨论】:

      • 这个“答案”中的代码还是错误的,请按照前面的文字进行编辑和更正。
      • 我不太确定我错过了什么。我使用了 .get() ,它返回 1 个结果,我可以从查询中引用电子邮件、密码、用户名和 id 值。返回的 JSON 是我在响应消息中寻找的内容。你能详细说明什么是“错误”吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-07
      • 2014-03-02
      • 1970-01-01
      相关资源
      最近更新 更多