Google app engine 如何让自定义ndb.Model方法工作(GAE和Python)
为什么以下两个例子都不起作用Google app engine 如何让自定义ndb.Model方法工作(GAE和Python),google-app-engine,model,python-2.7,Google App Engine,Model,Python 2.7,为什么以下两个例子都不起作用 class Person(ndb.Model): first_name = ndb.StringProperty() last_name = ndb.StringProperty() city = ndb.StringProperty() birth_year = ndb.IntegerProperty() height = ndb.IntegerProperty() @classmethod def get_person(s
class Person(ndb.Model):
first_name = ndb.StringProperty()
last_name = ndb.StringProperty()
city = ndb.StringProperty()
birth_year = ndb.IntegerProperty()
height = ndb.IntegerProperty()
@classmethod
def get_person(self, _last_name, _max_height):
a_person = Person.query(
ndb.AND(
Person.last_name == _last_name,
Person. height == _max_height
))
return a_person
另一个示例将Person
替换为self
:
@classmethod
def get_person(self, _last_name, _max_height):
a_person = self.query(
ndb.AND(
self.last_name == _last_name,
self. height == _max_height
))
return a_person
因此,基本上我希望能够使用last\u name
和max\u height
调用get\u person
方法,并让它返回一个person(假设只有一个匹配项)。我如何做到这一点
调用类/对象具有以下行:
my_person = Person.get_person('Doe',7)
if my_person.first_name is None:
my_person.first_name = 'John'
但是代码没有说明“我的人”没有属性“名字”(或我尝试的任何其他属性)。以下操作有效:
@classmethod
def get_person(self, _last_name, _max_height):
a_person = Person.query(
ndb.AND(
Person.last_name == _last_name,
Person. height == _max_height
))
return a_person.get()