【问题标题】:AttributeError: 'Article' object has no attribute 'learning_goals' -- attribute not being recognized for a specific modelAttributeError: 'Article' 对象没有属性 'learning_goals' -- 没有为特定模型识别属性
【发布时间】:2017-08-09 18:28:42
【问题描述】:

我正在使用用于 google app 引擎的 python 标准框架,但在从模型中获取属性时遇到了问题。

这是我正在使用的“文章”模型的模型类:

class Article(ndb.Model):
  # Entry metadata
  timestamp = ndb.KeyProperty(kind='Timestamp', repeated=True)

  # Article metadata
  authors = ndb.KeyProperty(kind='Author', repeated=True)
  title = ndb.StringProperty(indexed=False)
  journal = ndb.StringProperty(indexed=False)
  volume = ndb.StringProperty(indexed=False)
  number = ndb.StringProperty(indexed=False)
  pages = ndb.StringProperty(indexed=False)
  year = ndb.IntegerProperty(indexed=True)
  publisher =  ndb.StringProperty(indexed=False)
  # Methodology
  methodology = ndb.KeyProperty(kind='Methodology')
  learning_goals = ndb.KeyProperty(kind='LearningGoal', repeated=True, indexed=True)

  # Summary data
  type = ndb.StringProperty(indexed=True,choices=['Theoretical','Empirical','Review Article','Taxonomy Development','Practitioner', 'Other'], repeated=True)
  star = ndb.BooleanProperty(indexed=True,default=False)
  purpose = ndb.TextProperty(default="")
  findings = ndb.TextProperty(default="")
  recommendations = ndb.StringProperty(default="")
  citation = ndb.TextProperty(default="")
  audience = ndb.StringProperty(choices=['Practitioner', 'Researcher', 'Developer', 'Administrator', 'Other'], repeated=True)


  @property
  def author_names(self):
    return ndb.get_multi(self.authors)

  @property
 def _methodology(self):
    if self.methodology == None:
      methodology = Methodology()
      self.methodology = methodology.key
    else:
      methodology = self.methodology.get()
    return methodology


  @property
  def _learning_goal(self):
    return ndb.get_multi(self.learning_goals)

我遇到的问题是我的处理程序由于某种原因无法识别所有模型属性。我的处理程序类如下:

class ArticleCategoryHandler(webapp2.RequestHandler):
  def get(self,key):
    """ This is """
    article = ndb.Key(urlsafe=key).get()
    logging.info(article)
    logging.info('\n')
    template_values = {
      'key': key,
      'application_url': self.request.application_url,
      'user': users.get_current_user(),
      'url': users.create_logout_url(self.request.uri),
      'url_linktext': "Logout",
      'article': article,
      'categories': ['summary','learning-goals','methodology']
    }

    template = JINJA_ENVIRONMENT.get_template('templates/admin_category.html')
    self.response.write(template.render(template_values))

对于特定文章,logging.info(article) 列出 learning_goals 属性。但是,当我尝试执行 logging.info(article.learning_goals) 或 logging.info(article._learning_goal) 时,它给了我以下错误:

Traceback (most recent call last):
  File "/home/noah-banholzer/summer_research_2017/google-cloud-sdk/platform/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1535, in __call__
    rv = self.handle_exception(request, response, e)
  File "/home/noah-banholzer/summer_research_2017/google-cloud-sdk/platform/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1529, in __call__
    rv = self.router.dispatch(request, response)
  File "/home/noah-banholzer/summer_research_2017/google-cloud-sdk/platform/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher
    return route.handler_adapter(request, response)
  File "/home/noah-banholzer/summer_research_2017/google-cloud-sdk/platform/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1102, in __call__
    return handler.dispatch()
  File "/home/noah-banholzer/summer_research_2017/google-cloud-sdk/platform/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 572, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "/home/noah-banholzer/summer_research_2017/google-cloud-sdk/platform/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 570, in dispatch
    return method(*args, **kwargs)
  File "/home/noah-banholzer/summer_research_2017/everydaycomputing.org/site_database/admin_category.py", line 22, in get
    logging.info(article.learning_goals)
AttributeError: 'Article' object has no attribute 'learning_goals'

我已确保文章的 LearningGoal 属性已编入索引,并在本地开发服务器和实时应用程序上检查了这一点。出于某种原因,当我尝试在本地开发服务器的交互式控制台中执行类似的查询时,它识别出 Article 的 learning_goals 属性。此外,它识别文章模型的所有其他属性(即方法、标题等)。有没有其他人遇到过这个问题?

谢谢!

【问题讨论】:

  • 您的应用是否有多个服务/模块共享数据存储模型?
  • 是的,其他服务之一访问文章/学习目标模型。但是我不应该能够从不同的服务访问相同的模型吗?

标签: python google-app-engine


【解决方案1】:

服务在代码级别完全相互隔离。来自App Engine Services as microservices

在 App Engine 项目中,您可以将多个微服务部署为 单独的 services,以前在 App Engine 中称为 modules。 这些服务完全隔离代码;唯一的执行方式 这些服务中的代码是通过 HTTP 调用,例如用户 请求或 RESTful API 调用。一个服务中的代码不能直接调用 另一个服务中的代码。代码可以部署到服务 独立的,不同的服务可以写成不同的 语言,例如 Python、Java、Go 和 PHP。自动缩放,加载 平衡,机器实例类型都是独立管理的 服务。

这意味着您必须明确实现一种在需要访问相同实体类型的不同服务之间共享数据存储模型的方法。

我的建议是在每个服务中使用符号链接的模型定义文件,请参阅Sharing entities between App Engine modulesCommunicating between google app engine services

同样重要的是重新部署所有服务在模型更改时共享模型,否则未重新部署的服务将使用过时的模型定义运行,从而为类似的错误留下空间你举报了。

【讨论】:

  • 我的 .yaml 文件(app.yaml 和 site-database.yaml)都存在于同一个顶级目录中。因此,因为它们在同一个目录中,所以它们不应该共享目录中的所有内容吗?或者至少,这就是我从您的回答中得到的:stackoverflow.com/questions/42424494/… 他们必须被分成不同的目录,还是我误解了?
  • 是的,如果服务共享相同的目录,那么它们也共享模型。只需检查部署是否也同步。
猜你喜欢
  • 1970-01-01
  • 2016-06-05
  • 2018-06-12
  • 2015-04-28
  • 2021-10-20
  • 2019-05-11
  • 2020-09-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多