【发布时间】: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