AttributeError: 'Article' object has no at

2019-07-29 09:24发布

I am using the python standard framework for google app engine and I am running into issues with getting attributes from a model.

Here is my model class for the 'Article' model I am using:

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)

The problem I am getting is that my handler for some reason does not recognize all the model attributes. My handler class is as follows:

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

For a particular article, logging.info(article) lists the learning_goals attribute. However, when I try to do logging.info(article.learning_goals) or logging.info(article._learning_goal), it gives me the following error:

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'

I have made sure the LearningGoal attribute for articles is indexed, and checked this on both the local dev server and the live app. For some reason, when I try to do a similar query in the interactive console for the local development server, it recognizes the learning_goals attribute for Article. Additionally, it recognizes all the other attributes of the Article model (i.e. methodology, title, etc.). Has anyone else encountered this issue?

Thanks!

1条回答
beautiful°
2楼-- · 2019-07-29 10:04

Services are completely isolated from each-other at code level. From 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.

What this means is that you have to explicitly implement a way of sharing the datastore models across different services that need to access the same entity types.

My recommendation would be to have the file with the model definitions symlinked inside each of the services, see Sharing entities between App Engine modules and maybe Communicating between google app engine services.

Very important is also to re-deploy all services sharing the models whenever the models change, otherwise the services not re-deployed will be running with outdated model definitions, leaving room for errors like the one you reported.

查看更多
登录 后发表回答