本文介绍了如何获取Google App Engine中Python的StringProperty的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何获得nbd.Model的值?我想返回由几个字段组成的描述,但我无法使其工作。这是我的课程代码:

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

@classmethod $ b $ def get_description(self):
#returnKobe Bryant([email protected]
return self.name +'('+ self.email +')'

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

解决方案

$ c>在类 User 实例上访问是一个字符串,并且可以很好地附加到任何其他字符串。你明显的错误是在 classmethod 装饰器中,它使得该方法适用于,当它显然必须适用于特定的实例 strong>!

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 ([email protected])"
    return self.name + ' (' + self.email + ')'

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

解决方案

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!

这篇关于如何获取Google App Engine中Python的StringProperty的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 07:28