【问题标题】:AttributeError on google cloud datastore entity object谷歌云数据存储实体对象上的 AttributeError
【发布时间】:2016-04-29 13:59:21
【问题描述】:

我有一个名为 Author 的实体。下面是ndb类:

class Author(ndb.Model):
    name = ndb.StringProperty()
    website = ndb.StringProperty()
    bio = ndb.StringProperty()
    profile_image_url = ndb.StringProperty()
    slug = ndb.ComputedProperty(lambda self: make_slug(self.name))

我正在创建这样的作者对象

author = Author(id=pk, **author_dict)
author.put()

第一次创建作者对象时没有错误。

我正在检索这样的作者对象

author = Author.get_by_id(pk)

当我尝试访问作者简介字段时,我得到一个 AttributeError

author.bio = bio_text

访问现有作者对象上的字段时出现错误。错误不会出现在所有字段上。

控制台显示所有列。 bio 列存在于作者实体的数据存储控制台中。

我删除了所有作者实体并创建了新条目。我仍然收到 AttributeError。

我在数据存储区中有多个实体,但我只收到作者实体的此错误。

错误说明:

File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/ndb/model.py", 
line 2990, in _set_attributes
prop = getattr(cls, name)  # Raises AttributeError for unknown properties.
AttributeError: type object 'Author' has no attribute 'bio'

关闭:

我在另一个模块中有一个名为 Author 的类。 NDB 以某种方式使用此类来创建作者实体并导致此行为。

【问题讨论】:

  • 请您提供minimal reproducible example。我无法使用您提供的示例代码重现此问题。
  • 您好,我添加了更多信息。我可以添加周围的代码,但它与错误无关。
  • 只有当“author”是一个类而不是一个实例,并且是一个没有bio属性的类时,你才会得到这个错误。因此,您似乎在代码中的某处覆盖了author
  • 您可以尝试在失败的行之前打印或记录type(author)author.__name__,以查看author 实际上是什么。
  • 我尝试了你的建议,无论我在 Author 类中有什么字段,type(Author) 都在打印 Author

标签: google-app-engine app-engine-ndb google-cloud-datastore


【解决方案1】:

我在另一个模块中有一个名为 Author 的类。此类被用作另一个实体类中的结构化属性。

NDB 不知何故使用这个类来创建作者实体。我重命名了这个类,错误消失了。

现在我为所有 ndb 实体和结构化属性使用不同的类名。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-17
    • 2020-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多