如何在 Python 中为 Google App Engine 获取 StringProperty 的值?

如何在 Python 中为 Google App Engine 获取 StringProperty 的值?

问题描述:

如何获取 nbd.Model 的值?我想返回一个由几个字段组成的描述,但我无法让它工作.这是我的班级代码:

How do I get the values of a nbd.Model? I want to return a description that is composed of several of the fields but I can't get it to work. This is my code for my class:

class User(ndb.Model):
  name = ndb.StringProperty()
  email = ndb.StringProperty()

  @classmethod
  def get_description(self):
    # return "Kobe Bryant (kobe.bryant@lakers.net)"
    return self.name + ' (' + self.email + ')'

但 name 是一个 StringProperty 对象,不能附加到字符串.如何获取 StringProperty 的值?

But name is a StringProperty object and can't be appended to a string. How do I get the value of the StringProperty?

实际上,self.nameUser 类的实例上访问 一个字符串,可以完美地附加到任何其他字符串.您明显的错误在于 classmethod 装饰器,当它显然必须应用于特定的实例时,它使该方法应用于

Actually, self.name accessed on an instance of class User is a string and can be perfectly well appended to any other string. Your obvious mistake is in that classmethod decorator which makes the method apply to the class when it clearly must apply to a specific instance!