如何在 Python 中为 Google App Engine 获取 StringProperty 的值? [英] How do I get the value of a StringProperty in Python for Google App Engine?

查看:30
本文介绍了如何在 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!

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

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆