2012-12-04 33 views
2

爲什麼既不是下面的例子工作?如何獲得定製ndb.Model方法的工作(GAE和Python)

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_personlast_namemax_height,並使其返回一個人(假設只有一個比賽)。我如何實現這一目標?

調用的類/對象有下面幾行:

my_person = Person.get_person('Doe',7) 
if my_person.first_name is None: 
    my_person.first_name = 'John' 

但是代碼失敗說my_person沒有屬性first_name(或任何其他屬性我試試)。

回答

3

以下工作:

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