2011-08-07 2 views
1

내 Google App Engine - Python 앱에 내 모델의 JSON 직렬화를 허용하는 to_dict()가 있습니다.Python - Google App Engine - Dict에서 모델 ID 가져 오기

class BaseModel(db.Model): 
    createdOn = db.DateTimeProperty(auto_now_add=True) 
    createdBy = db.UserProperty(auto_current_user_add=True) 
    modifiedOn = db.DateTimeProperty(auto_now_add=True) 
    modifiedBy = db.UserProperty(auto_current_user=True) 

    def to_dict(self): 
    return dict([(p, unicode(getattr(self, p))) for p in self.properties()]) 

어떻게 modelInstance.key(). ID()는 딕셔너리의 JSON 개체의 따라서 일부의 일부임을 확인합니까? ext.db에서 편리한 to_dict 방법이있다

+0

기본 키 필드가이 사전에 포함되기를 원하십니까? –

+0

데이터 저장소는 dicts를 저장할 수 있어야합니다. 해결 방법이 있지만 [Google] (http://code.google.com/p/googleappengine/issues/detail?id=805)에는 이미 기능 요청이 있습니다. 이 기능을 사용하려면 이슈 번호 옆의 별표를 클릭하여 투표하십시오. –

답변

5

, 당신은 같은 당신의 방법을 업데이트 할 수 있습니다 :

class MyModel(db.Model): 
    def to_dict(self): 
     return db.to_dict(self, {'id':self.key().id()}) 
당신은 당신의 모델이 비록 ID를 얻기 위해 시도하기 전에 is_saved되어 있는지 확인 할 수 있습니다

.

관련 문제