AttributeError:'Article'对象没有属性'learning_goals' - 特定模型无法识别属性

时间:2017-08-09 18:28:43

标签: python google-app-engine

我正在使用谷歌应用引擎的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属性。此外,它还识别文章模型的所有其他属性(即方法,标题等)。还有其他人遇到过这个问题吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

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

  

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

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

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

非常重要的是,只要模型发生变化,重新部署所有服务共享模型,否则未重新部署的服务将使用过时的模型定义运行,从而为错误留出空间你报道了。