我最近從Google App Engine上的ext.db切換到NDB.model,對於數據庫來說還是比較新的。以下是新的NDB代碼,其大部分與ext.db保持不變。GAE NDB如何獲取用戶數據並返回到數據庫
我想要做的是當用戶嘗試登錄時,檢查是否存在uid。如果是這樣,請將數據庫中的數據作爲「條目」傳回。如果uid不存在,則從Json中提取數據並填充新的「條目」。
class User(ndb.Model):
uid = ndb.StringProperty(required = True)
firstName = ndb.StringProperty(required = True)
lastName = ndb.StringProperty(required = True)
emailAddress = ndb.StringProperty(required = True)
password = ndb.StringProperty(required = True)
created = ndb.DateTimeProperty(auto_now_add = True)
last_modified = ndb.DateTimeProperty(auto_now = True)
@classmethod
def addUser(cls, info):
entry = User.query().filter(Profile.uid == info['uid']).get
#USED TO BE FOR DB: entry = User.all().filter('uid = ', info['uid']).get()
# if the profile doesn't exist, create it with basic information
if entry == None:
entry = User(uid = info["id"],
firstName = info["firstName"],
lastName = info["lastName"],
emailAddress = info["emailAddress"],
password = info[passwd])
# At this point, we have a valid entry.
entry.last_modified = datetime.now()
entry.put()
return entry
此代碼不起作用。我曾嘗試entry = User.query().filter(Profile.uid == info['uid']).get
但我得到一個屬性錯誤
AttributeError: 'instancemethod' object has no attribute 'last_modified'
我道歉無法澄清更多的,但我堅持。我錯過了什麼?一切工作正常與ext.db(注意,我改變了上面的代碼中指出),但我不明白NDB有什麼問題。我也在尋求改進,所以請建議更改。
任何幫助,非常感謝!謝謝。
啊,那就是訣竅。謝謝。 – jbro