AttributeError:“Article”对象没有特定mod的“learning\u goals”属性

2024-09-23 06:38:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用googleappengine的python标准框架,在从模型中获取属性时遇到了问题。你知道吗

下面是我正在使用的“Article”模型的模型类:

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))

对于特定的文章,日志记录.info(article)列出了learning\u goals属性。但是,当我尝试日志记录.info(第1条学习目标)或者日志记录.info(文章.\u学习\u目标),它给了我以下错误:

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属性被编入索引,并在本地dev服务器和live应用程序上进行了检查。出于某种原因,当我尝试在交互式控制台中为本地开发服务器执行类似查询时,它会识别文章的learning\u goals属性。此外,它还识别文章模型的所有其他属性(即方法、标题等)。还有其他人遇到过这个问题吗?你知道吗

谢谢!你知道吗


Tags: selffalsetruehomegetreturngoogleindexed
1条回答
网友
1楼 · 发布于 2024-09-23 06:38:08

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

In an App Engine project, you can deploy multiple microservices as separate services, previously known as modules in App Engine. These services have full isolation of code; the only way to execute code in these services is through an HTTP invocation, such as a user request or a RESTful API call. Code in one service can't directly call code in another service. Code can be deployed to services independently, and different services can be written in different languages, such as Python, Java, Go, and PHP. Autoscaling, load balancing, and machine instance types are all managed independently for services.

这意味着您必须显式地实现一种跨需要访问相同实体类型的不同服务共享数据存储模型的方法。你知道吗

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

非常重要的一点是重新部署所有服务在模型更改时共享模型,否则未重新部署的服务将使用过时的模型定义运行,从而为您报告的错误留下空间。你知道吗

相关问题 更多 >