gpt4 book ai didi

google-app-engine - 如何让自定义 ndb.Model 方法工作(GAE 和 Python)

转载 作者:行者123 更新时间:2023-12-04 05:20:55 25 4
gpt4 key购买 nike

为什么以下示例都不起作用?

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

另一个示例替换 Personself :
@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

所以基本上我希望能够调用 get_person使用 last_name 的方法和 max_height并让它返回一个人(假设只有一个匹配项)。我该如何做到这一点?

调用类/对象有以下几行:
my_person = Person.get_person('Doe',7)
if my_person.first_name is None:
my_person.first_name = 'John'

但是代码失败了 my_person没有属性 first_name (或我尝试的任何其他属性)。

最佳答案

以下作品:

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

关于google-app-engine - 如何让自定义 ndb.Model 方法工作(GAE 和 Python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13693964/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com