python - Google Cloud Datastore 'NoneType' 对象没有属性 'email'

标签 python python-2.7 google-app-engine google-cloud-datastore webapp2

当我尝试从数据存储中获取一种类型时,它返回 NoneType,就好像查询为空一样。我知道数据存储在保存时工作正常,但从查询中提取类型却不能。

此外,在 Google 云控制台网站中使用 GQL 查询并使用 SELECT * FROM User 确实会返回所有类型。用户类型没有 parent ,它位于根。我确保所有属性也都编入索引。

我不确定我在 GET 上做错了什么。

MyApp.py

import webapp2
from google.appengine.ext import ndb
from google.appengine.ext.db import GqlQuery


class MainHandler(webapp2.RequestHandler):

    def post(self):
        message = self.request.body
        message = message.splitlines()

        if message[0] == "register":
            user = User.create_user(message[1], message[2], message[3])
            user_key = User.save_user(user)
            if user_key is not None:
                self.response.write(user_key)

        else:
            user = User.get_by_id(User.email == message[0])
            if User.token == message[1]:
                self.response.write("CURRENT")
            else:
                User.token = message[1]
                User.save_user(user)
                self.response.write("UPDATED")

    def get(self):
        self.response.write("CONNECTED")
        user= User.query().get()
        self.response.write("\n" + query.email)


class User(ndb.Model):
    email = ndb.StringProperty()
    token = ndb.StringProperty()
    name = ndb.StringProperty()

    @classmethod
    def create_user(cls, email, token, name):
        user = User(email=email, token=token, name=name, id=email)
        return user

    @classmethod
    def save_user(cls, user):
        user_key = user.put()
        return user_key

    @classmethod
    def get_user(cls, email):
        return User.get_by_id(User.email == email)


app = webapp2.WSGIApplication([
    ('/', MainHandler)
], debug=True)

最佳答案

您似乎将 .get_by_id() 与查询混淆了。

get_by_id方法实际上映射到ndb.Model._get_by_id,它调用ndb.Model._get_by_id_async,这需要实体 key 标识符确定用于执行直接实体查找(而不是查询!)的实体键。来自appengine.ext.ndb.model.py:

  @classmethod
  @utils.positional(3)
  def _get_by_id_async(cls, id, parent=None, app=None, namespace=None,
                       **ctx_options):
    """Returns an instance of Model class by ID (and app, namespace).

    This is the asynchronous version of Model._get_by_id().
    """
    key = Key(cls._get_kind(), id, parent=parent, app=app, namespace=namespace)
    return key.get_async(**ctx_options)

但是在您的代码中,您将 bool 作为 id 传递:User.email == message[0],它很可能与任何现有实体都不匹配键标识符,因此 None 结果会导致您看到的错误。

由于您拥有的可用信息是实体属性的值(email 值),因此您可能想要执行查询,大致如下:

results = User.query(User.email == message[0]).fetch(limit=1)
if results:
   user = results[0]

关于python - Google Cloud Datastore 'NoneType' 对象没有属性 'email',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50752283/

相关文章:

python - 使用 bash 文件运行两个 python 脚本

python - pip install `unicode` 错误中加密安装失败

python - 如何使用 Scipy 绘制真实曲线

python - max 在集合列表上做什么?

java - Google App Engine、JDO 和 equals/hashCode

python - 对 AppEngine 进行单元测试以发现故障

python - 用前后的平均值填充包含 NaN 的单元格

python - 最短路线优化不起作用

python - 有没有办法强制 Python 程序在 2.7 版中运行?

python - 如何将 Python POST 数据转换为 JSON?