2012-06-30 4 views
0

내가 파이썬에서이 코드와 구글 앱 스토어에서 기업의 특정 모델을 호출하는 것을 시도하고있다 :형식 오류 : '키'개체가 아닙니다 호출

class Profile(BlogHandler): 
    def get(self): 
     user = users.get_current_user() 

     if user: 
      user = users.get_current_user() 
      user_db_qry = User.query(User.theid == user.federated_identity()) 
      #theid is how I have saved each user's federated id in the User entity in the datastore 
      user_db_list = user_db_qry.fetch(1) 
      profile_user = user_db_list[0] 
      profile_id = profile_user.key().id_or_name() 
      #I am trying to get the datastore created ID/Name here and this is the line that gives me the error 

      self.redirect('/profile/%s' % str(profile_id)) 
     else: 
      self.redirect("/about") 

그래서 내가 무슨 일이 일어나고 있는지 아주 확실하지 않다 잘못된. 내 검색어가 올바르지 않습니까?

답변

4

서문으로 말하면 나는 GAE에 대한 경험이 없습니다. 이 답변은 API 문서에만 기반합니다.

docs는 key() 메서드가 인 모델 인스턴스 만 있다고 주장합니다. 하지만 User class은 고유 한 종류의 엔티티 인 것 같습니다. 당신이 사용자의 key 멤버에 포함 된 내용을 조사하려면

profile_id = profile_user.user_id() 
profile_nick = profile_user.nickname() 

것은, 당신이 선택하여 디버깅 할 수 대신 핵심 인스턴스에 액세스하려고의

, 당신은 단지 직접적인 방법을 사용할 수 있습니다 그것을 아웃 :

print type(profile_user.key) 
print dir(profile_user.key) 

업데이트는 요 것이 분명 귀하의 의견에

u는이 문제를 설명하는 NDB variants을 사용하고 있습니다. NDB Key Class은 DB Key Class과 다릅니다. 그것에는 id_or_name() 방법이 없습니다. 그것이 가지고있는 것 :

id() Returns the string or integer id in the last (kind, id) pair, or None if the key is incomplete.

string_id() Returns the string id in the last (kind, id) pair, or None if the key has an integer id or is incomplete.

integer_id() Returns the integer id in the last (kind, id) pair, or None if the key has an string id or is incomplete.

+0

user_id ()는 Google 사용자의 고유 한 주소와 다른 사용자의 주소 중 하나만 반환합니다. 내 자신의 User 클래스와 데이터 저장소의 ID 메서드를 사용하고 있습니다. – exployre

+0

@exployre : Amber의 대답에 대한 귀하의 의견이 더 명확하게 설명했습니다. – jdi

+0

+1이 최종 목표를 달성하는 데 훨씬 더 세부적으로 적용되기 때문에 +1로 업데이트되었습니다. – Amber

2
 profile_id = profile_user.key().id_or_name() 

에는 첫 번째 괄호가 필요하지 않습니다.

 profile_id = profile_user.key.id_or_name() 
+0

그러면 profile_id = profile_user.key로 알려줍니다.id_or_name() AttributeError : 데이터 저장소 뷰어로 이동할 때 '키'객체에 'id_or_name'속성이 없습니다. 사용자 객체에 ID/이름이 자동으로 생성 된 것을 볼 수 있습니다. – exployre

+0

사실 사용자 (db.model)가 아닌 사용자 (nbd.model)를 사용하고 있습니까? – exployre

+0

예. http://stackoverflow.com/questions/11875903/how-do-i-get-the-key-for-the-current-record-in-gae-ndb-in-a-python-for-loop –

0

당신이 다른 곳에서 쓴 것을 바탕으로 어떻게 엔티티 용 키를 할당 했습니까? 대신 당신은 그 (것)들에게 이름을주고 있습니까?

이 같은 끝나는 키 경로 사용하는 경우 (참고 : 나는하지 평균 부모를 할 , 단지 키 경로) 당신의 키

ndb.Key.from_path(..."User", "harold") 

을, 다음 키 없음을 반환하지 않습니다 자신의 ID는 이어야합니다. (db과 비슷하게 작동하는 경우 ndb)은 이름 속성이 있어야합니다.

이것을 알아내는 가장 쉬운 방법은 SDK에서 앱을 실행하고 http://localhost:8080/_ah/admin으로 이동하여 대화 형 콘솔을 열고 원하는 것을 얻을 때까지 속성을 시도해보십시오 (아마도 모두). 앱 엔진을 사용하면 이상한 오류를 잡는 데 도움이되는 경우가 종종 있습니다 (예 : 위치 지정 인수 대신 kewyord 인수를 사용하려고하면 API 호출 중 많은 수가 실패합니다)

ndb.Key.from_path()에서 생성 한 키의 조회를 사용자 인스턴스에 보내고 키를 올바르게 인스턴스화/가져오고 있는지 확인하십시오.