【发布时间】:2014-04-02 09:19:57
【问题描述】:
我最近在 Google App Engine 上从 ext.db 切换到了 NDB.model,但总的来说,我对数据库还是很陌生。下面是新的 NDB 代码,与 ext.db 基本没有变化。
我想要做的是当用户尝试登录时,检查 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' 对象没有属性 'last_modified'
我很抱歉无法澄清更多,但我被困住了。我错过了什么? ext.db 一切正常(请注意我在上面的代码中所做的更改),但我看不出 NDB 有什么问题。我也在寻求改进,所以请提出更改建议。
非常感谢任何帮助!谢谢。
【问题讨论】:
标签: python google-app-engine database-design app-engine-ndb